NWG: Small application layout
This section will go in details on how to origanize a small NWG application.
An application can be considered small when it has one interface and less than 10 controls. For example,
all NWG examples are classified as small applications while the test suite is not.
Note that you don't
really have to follow the whole "NativeUi" setup for very small applications, for example see
the barebone example.
Overview
- Define a struct that will hold your non-gui application data
- Define another struct that will hold the GUI data
- Add your application struct to the GUI struct members behind a Refcell
- Define the gui functions by
impl
the GUI struct
- Implement
NativeUi
for the GUI struct (manually or with NWD)
- In the main funtion, initialize the application struct and the GUI struct
- Call
build_ui
with the initialized data
- Start processing events
Defining the structs
Assuming your application has data that must be stored.
Next, it's time to define the UI struct.
Implementing NativeUI
Assuming you're not using native-windows-derive, you will have to manually implement the NativeUi trait.
Initializing
nwg::init().expect("Failed to init Native Windows GUI");
let data: RefCell<MyData> = { /* Initializing... */ };
let app: MyApp = MyApp { data, ..Default::default() };
let _ui = MyApp::build_ui(app).expect("Failed to build UI");
nwg::dispatch_thread_events();
Once everything is done, there's only initialization that's left to do. As this point everything is straightforward so...
1. Call the
init
function of nwg.
2. Initialize the data struct and the Ui struct.
3. Call the
build_ui
method defined by
NativeUi
.
4. As the final touch, call
dispatch_thread_events
to dispatch the events to the application