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

  1. Define a struct that will hold your non-gui application data
  2. Define another struct that will hold the GUI data
  3. Add your application struct to the GUI struct members behind a Refcell
  4. Define the gui functions by impl the GUI struct
  5. Implement NativeUi for the GUI struct (manually or with NWD)
  6. In the main funtion, initialize the application struct and the GUI struct
  7. Call build_ui with the initialized data
  8. 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