Native Windows Derive: Basics
Natives Windows Derive (NWD) is a procedural macro that can be used to generate the NWG boilerplate code.
The code generated by NWD will be as fast as hand written code because NWD do not take any assumptions about your code.
The first thing to do is to add native-windows-derive to your Cargo dependencies:
[dependencies]
native-windows-gui = "1.0.12"
native-windows-derive = "1.0.3"
And then, in
main.rs
or
lib.rs
:
extern crate native_windows_gui as nwg;
extern crate native_windows_derive as nwd;
use nwd::NwgUi;
NWD expects your code to use a struct-based data layout, the same that has been used through this guide. If you skipped
3.4 Small application layout, you should go read it first.
Basic setup
With this out of the way, derive
NwgUi
from your GUI struct:
#[derive(NwgUi)]
pub struct BasicApp {
...
}
This will implement the base of the
NativeUi
trait on a new Ui wrapper struct.
The new struct will be named
[StructName]Ui
,
in this case:
BasicAppUi
.
This structure will be returned by the
BasicApp::build_ui
function.
The derive macro will also implement
Deref
for you.
And that's only the first step. NWD has no way to know which fields in your struct are controls, it doesn't how to initalize them, it doesn't know
how to dispatch the events, etc.
In order to feed NWD this information, the GUI fields needs to be marked with the NWD attributes. The following sections will go over these.