Struct druid::WindowDesc[][src]

pub struct WindowDesc<T> {
    pub id: WindowId,
    // some fields omitted
}
Expand description

A description of a window to be instantiated.

Fields

id: WindowId

The WindowId that will be assigned to this window.

This can be used to track a window from when it is launched and when it actually connects.

Implementations

Create a new WindowDesc, taking the root Widget for this window.

Examples found in repository
druid/examples/layout.rs (line 83)
82
83
84
85
86
87
88
89
pub fn main() {
    let window = WindowDesc::new(build_app()).title("Very flexible");

    AppLauncher::with_window(window)
        .log_to_console()
        .launch(0)
        .expect("launch failed");
}
More examples
druid/examples/either.rs (line 51)
50
51
52
53
54
55
56
pub fn main() {
    let main_window = WindowDesc::new(ui_builder()).title("Switcheroo");
    AppLauncher::with_window(main_window)
        .log_to_console()
        .launch(AppState::default())
        .expect("launch failed");
}
druid/examples/custom_widget.rs (line 157)
156
157
158
159
160
161
162
pub fn main() {
    let window = WindowDesc::new(CustomWidget {}).title(LocalizedString::new("Fancy Colors"));
    AppLauncher::with_window(window)
        .log_to_console()
        .launch("Druid + Piet".to_string())
        .expect("launch failed");
}
druid/examples/blocking_function.rs (line 107)
105
106
107
108
109
110
111
112
fn main() {
    let main_window =
        WindowDesc::new(ui_builder()).title(LocalizedString::new("Blocking functions"));
    AppLauncher::with_window(main_window)
        .delegate(Delegate {})
        .launch(AppState::default())
        .expect("launch failed");
}
druid/examples/split_demo.rs (line 83)
82
83
84
85
86
87
88
89
pub fn main() {
    let window = WindowDesc::new(build_app())
        .title(LocalizedString::new("split-demo-window-title").with_placeholder("Split Demo"));
    AppLauncher::with_window(window)
        .log_to_console()
        .launch(0u32)
        .expect("launch failed");
}
druid/examples/scroll.rs (line 28)
27
28
29
30
31
32
33
34
pub fn main() {
    let window = WindowDesc::new(build_widget())
        .title(LocalizedString::new("scroll-demo-window-title").with_placeholder("Scroll demo"));
    AppLauncher::with_window(window)
        .log_to_console()
        .launch(0u32)
        .expect("launch failed");
}

Set the title for this window. This is a LabelText; it can be either a String, a LocalizedString, or a closure that computes a string; it will be kept up to date as the application’s state changes.

Examples found in repository
druid/examples/layout.rs (line 83)
82
83
84
85
86
87
88
89
pub fn main() {
    let window = WindowDesc::new(build_app()).title("Very flexible");

    AppLauncher::with_window(window)
        .log_to_console()
        .launch(0)
        .expect("launch failed");
}
More examples
druid/examples/either.rs (line 51)
50
51
52
53
54
55
56
pub fn main() {
    let main_window = WindowDesc::new(ui_builder()).title("Switcheroo");
    AppLauncher::with_window(main_window)
        .log_to_console()
        .launch(AppState::default())
        .expect("launch failed");
}
druid/examples/custom_widget.rs (line 157)
156
157
158
159
160
161
162
pub fn main() {
    let window = WindowDesc::new(CustomWidget {}).title(LocalizedString::new("Fancy Colors"));
    AppLauncher::with_window(window)
        .log_to_console()
        .launch("Druid + Piet".to_string())
        .expect("launch failed");
}
druid/examples/blocking_function.rs (line 107)
105
106
107
108
109
110
111
112
fn main() {
    let main_window =
        WindowDesc::new(ui_builder()).title(LocalizedString::new("Blocking functions"));
    AppLauncher::with_window(main_window)
        .delegate(Delegate {})
        .launch(AppState::default())
        .expect("launch failed");
}
druid/examples/split_demo.rs (line 84)
82
83
84
85
86
87
88
89
pub fn main() {
    let window = WindowDesc::new(build_app())
        .title(LocalizedString::new("split-demo-window-title").with_placeholder("Split Demo"));
    AppLauncher::with_window(window)
        .log_to_console()
        .launch(0u32)
        .expect("launch failed");
}
druid/examples/scroll.rs (line 29)
27
28
29
30
31
32
33
34
pub fn main() {
    let window = WindowDesc::new(build_widget())
        .title(LocalizedString::new("scroll-demo-window-title").with_placeholder("Scroll demo"));
    AppLauncher::with_window(window)
        .log_to_console()
        .launch(0u32)
        .expect("launch failed");
}

Set the menu for this window.

menu is a callback for creating the menu. Its first argument is the id of the window that will have the menu, or None if it’s creating the root application menu for an app with no menus (which can happen, for example, on macOS).

Examples found in repository
druid/examples/multiwin.rs (line 39)
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
pub fn main() {
    let main_window = WindowDesc::new(ui_builder()).menu(make_menu).title(
        LocalizedString::new("multiwin-demo-window-title").with_placeholder("Many windows!"),
    );
    AppLauncher::with_window(main_window)
        .delegate(Delegate {
            windows: Vec::new(),
        })
        .log_to_console()
        .launch(State::default())
        .expect("launch failed");
}

fn ui_builder() -> impl Widget<State> {
    let text = LocalizedString::new("hello-counter")
        .with_arg("count", |data: &State, _env| data.menu_count.into());
    let label = Label::new(text);
    let inc_button =
        Button::<State>::new("Add menu item").on_click(|_ctx, data, _env| data.menu_count += 1);
    let dec_button = Button::<State>::new("Remove menu item")
        .on_click(|_ctx, data, _env| data.menu_count = data.menu_count.saturating_sub(1));
    let new_button = Button::<State>::new("New window").on_click(|ctx, _data, _env| {
        ctx.submit_command(sys_cmds::NEW_FILE.to(Global));
    });
    let quit_button = Button::<State>::new("Quit app").on_click(|_ctx, _data, _env| {
        Application::global().quit();
    });

    let mut col = Flex::column();
    col.add_flex_child(Align::centered(Padding::new(5.0, label)), 1.0);
    let mut row = Flex::row();
    row.add_child(Padding::new(5.0, inc_button));
    row.add_child(Padding::new(5.0, dec_button));
    col.add_flex_child(Align::centered(row), 1.0);
    let mut row = Flex::row();
    row.add_child(Padding::new(5.0, new_button));
    row.add_child(Padding::new(5.0, quit_button));
    col.add_flex_child(Align::centered(row), 1.0);
    let content = ControllerHost::new(col, ContextMenuController);
    Glow::new(content)
}

struct Glow<W> {
    inner: W,
}

impl<W> Glow<W> {
    pub fn new(inner: W) -> Glow<W> {
        Glow { inner }
    }
}

impl<W: Widget<State>> Widget<State> for Glow<W> {
    fn event(&mut self, ctx: &mut EventCtx, event: &Event, data: &mut State, env: &Env) {
        self.inner.event(ctx, event, data, env);
    }

    fn lifecycle(&mut self, ctx: &mut LifeCycleCtx, event: &LifeCycle, data: &State, env: &Env) {
        if let LifeCycle::HotChanged(_) = event {
            ctx.request_paint();
        }
        self.inner.lifecycle(ctx, event, data, env);
    }

    fn update(&mut self, ctx: &mut UpdateCtx, old_data: &State, data: &State, env: &Env) {
        if old_data.glow_hot != data.glow_hot {
            ctx.request_paint();
        }
        self.inner.update(ctx, old_data, data, env);
    }

    fn layout(
        &mut self,
        ctx: &mut LayoutCtx,
        bc: &BoxConstraints,
        data: &State,
        env: &Env,
    ) -> Size {
        self.inner.layout(ctx, bc, data, env)
    }

    fn paint(&mut self, ctx: &mut PaintCtx, data: &State, env: &Env) {
        if data.glow_hot && ctx.is_hot() {
            BackgroundBrush::Color(Color::rgb8(200, 55, 55)).paint(ctx, data, env);
        }
        self.inner.paint(ctx, data, env);
    }
}

struct ContextMenuController;
struct Delegate {
    windows: Vec<WindowId>,
}

impl<W: Widget<State>> Controller<State, W> for ContextMenuController {
    fn event(
        &mut self,
        child: &mut W,
        ctx: &mut EventCtx,
        event: &Event,
        data: &mut State,
        env: &Env,
    ) {
        match event {
            Event::MouseDown(ref mouse) if mouse.button.is_right() => {
                ctx.show_context_menu(make_context_menu(), mouse.pos);
            }
            _ => child.event(ctx, event, data, env),
        }
    }
}

impl AppDelegate<State> for Delegate {
    fn command(
        &mut self,
        ctx: &mut DelegateCtx,
        _target: Target,
        cmd: &Command,
        data: &mut State,
        _env: &Env,
    ) -> Handled {
        if cmd.is(sys_cmds::NEW_FILE) {
            let new_win = WindowDesc::new(ui_builder())
                .menu(make_menu)
                .window_size((data.selected as f64 * 100.0 + 300.0, 500.0));
            ctx.new_window(new_win);
            Handled::Yes
        } else {
            Handled::No
        }
    }
More examples
druid/examples/textbox.rs (line 51)
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
pub fn main() {
    // describe the main window
    let main_window = WindowDesc::new(build_root_widget())
        .title(WINDOW_TITLE)
        .menu(make_menu)
        .window_size((400.0, 600.0));

    // create the initial app state
    let initial_state = AppState {
        single: "".to_string().into(),
        multi: "".to_string().into(),
    };

    // start the application
    AppLauncher::with_window(main_window)
        .log_to_console()
        .launch(initial_state)
        .expect("Failed to launch application");
}
druid/examples/markdown_preview.rs (line 100)
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
pub fn main() {
    // describe the main window
    let main_window = WindowDesc::new(build_root_widget())
        .title(WINDOW_TITLE)
        .menu(make_menu)
        .window_size((700.0, 600.0));

    // create the initial app state
    let initial_state = AppState {
        raw: TEXT.to_owned(),
        rendered: rebuild_rendered_text(TEXT),
    };

    // start the application
    AppLauncher::with_window(main_window)
        .log_to_console()
        .delegate(Delegate)
        .launch(initial_state)
        .expect("Failed to launch application");
}

Set the window size policy

Set the window’s initial drawing area size in display points.

You can pass in a tuple (width, height) or a Size, e.g. to create a window with a drawing area 1000dp wide and 500dp high:

window.window_size((1000.0, 500.0));

The actual window size in pixels will depend on the platform DPI settings.

This should be considered a request to the platform to set the size of the window. The platform might increase the size a tiny bit due to DPI.

Examples found in repository
druid/examples/transparency.rs (line 34)
31
32
33
34
35
36
37
38
39
40
41
42
43
pub fn main() {
    let window = WindowDesc::new(build_root_widget())
        .show_titlebar(false)
        .window_size((512., 512.))
        .transparent(true)
        .resizable(true)
        .title("Transparent background");

    AppLauncher::with_window(window)
        .log_to_console()
        .launch(HelloState { name: "".into() })
        .expect("launch failed");
}
More examples
druid/examples/multiwin.rs (line 162)
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
fn command(
        &mut self,
        ctx: &mut DelegateCtx,
        _target: Target,
        cmd: &Command,
        data: &mut State,
        _env: &Env,
    ) -> Handled {
        if cmd.is(sys_cmds::NEW_FILE) {
            let new_win = WindowDesc::new(ui_builder())
                .menu(make_menu)
                .window_size((data.selected as f64 * 100.0 + 300.0, 500.0));
            ctx.new_window(new_win);
            Handled::Yes
        } else {
            Handled::No
        }
    }
druid/examples/calc.rs (line 250)
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
pub fn main() {
    let window = WindowDesc::new(build_calc())
        .window_size((223., 300.))
        .resizable(false)
        .title(
            LocalizedString::new("calc-demo-window-title").with_placeholder("Simple Calculator"),
        );
    let calc_state = CalcState {
        value: "0".to_string(),
        operand: 0.0,
        operator: 'C',
        in_num: false,
    };
    AppLauncher::with_window(window)
        .log_to_console()
        .launch(calc_state)
        .expect("launch failed");
}
druid/examples/hello.rs (line 37)
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
pub fn main() {
    // describe the main window
    let main_window = WindowDesc::new(build_root_widget())
        .title("Hello World!")
        .window_size((400.0, 400.0));

    // create the initial app state
    let initial_state: HelloState = HelloState {
        name: "World".into(),
    };

    // start the application. Here we pass in the application state.
    AppLauncher::with_window(main_window)
        .log_to_console()
        .launch(initial_state)
        .expect("Failed to launch application");
}
druid/examples/textbox.rs (line 52)
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
pub fn main() {
    // describe the main window
    let main_window = WindowDesc::new(build_root_widget())
        .title(WINDOW_TITLE)
        .menu(make_menu)
        .window_size((400.0, 600.0));

    // create the initial app state
    let initial_state = AppState {
        single: "".to_string().into(),
        multi: "".to_string().into(),
    };

    // start the application
    AppLauncher::with_window(main_window)
        .log_to_console()
        .launch(initial_state)
        .expect("Failed to launch application");
}
druid/examples/image.rs (line 211)
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
pub fn main() {
    let main_window = WindowDesc::new(make_ui())
        .window_size((650., 450.))
        .title("Flex Container Options");

    let state = AppState {
        fill_strat: FillStrat::Cover,
        interpolate: true,
        interpolation_mode: InterpolationMode::Bilinear,
        fix_width: true,
        width: 200.,
        fix_height: true,
        height: 100.,
    };

    AppLauncher::with_window(main_window)
        .log_to_console()
        .launch(state)
        .expect("Failed to launch application");
}

Set the window’s minimum drawing area size in display points.

The actual minimum window size in pixels will depend on the platform DPI settings.

This should be considered a request to the platform to set the minimum size of the window. The platform might increase the size a tiny bit due to DPI.

To set the window’s initial drawing area size use window_size.

Examples found in repository
druid/examples/timer.rs (line 127)
121
122
123
124
125
126
127
128
129
130
131
132
133
134
pub fn main() {
    let window = WindowDesc::new(TimerWidget {
        timer_id: TimerToken::INVALID,
        simple_box: WidgetPod::new(SimpleBox),
        pos: Point::ZERO,
    })
    .with_min_size((200., 200.))
    .title(LocalizedString::new("timer-demo-window-title").with_placeholder("Look at it go!"));

    AppLauncher::with_window(window)
        .log_to_console()
        .launch(0u32)
        .expect("launch failed");
}
More examples
druid/examples/flex.rs (line 325)
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
pub fn main() {
    let main_window = WindowDesc::new(make_ui())
        .window_size((720., 600.))
        .with_min_size((620., 300.))
        .title("Flex Container Options");

    let demo_state = DemoState {
        input_text: "hello".into(),
        enabled: false,
        volume: 0.0,
    };

    let params = Params {
        axis: FlexType::Row,
        cross_alignment: CrossAxisAlignment::Center,
        main_alignment: MainAxisAlignment::Start,
        debug_layout: false,
        fix_minor_axis: false,
        fix_major_axis: false,
        spacers: Spacers::None,
        spacer_size: DEFAULT_SPACER_SIZE,
        fill_major_axis: false,
    };

    AppLauncher::with_window(main_window)
        .log_to_console()
        .launch(AppState { demo_state, params })
        .expect("Failed to launch application");
}

Builder-style method to set whether this window can be resized.

Examples found in repository
druid/examples/transparency.rs (line 36)
31
32
33
34
35
36
37
38
39
40
41
42
43
pub fn main() {
    let window = WindowDesc::new(build_root_widget())
        .show_titlebar(false)
        .window_size((512., 512.))
        .transparent(true)
        .resizable(true)
        .title("Transparent background");

    AppLauncher::with_window(window)
        .log_to_console()
        .launch(HelloState { name: "".into() })
        .expect("launch failed");
}
More examples
druid/examples/calc.rs (line 251)
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
pub fn main() {
    let window = WindowDesc::new(build_calc())
        .window_size((223., 300.))
        .resizable(false)
        .title(
            LocalizedString::new("calc-demo-window-title").with_placeholder("Simple Calculator"),
        );
    let calc_state = CalcState {
        value: "0".to_string(),
        operand: 0.0,
        operator: 'C',
        in_num: false,
    };
    AppLauncher::with_window(window)
        .log_to_console()
        .launch(calc_state)
        .expect("launch failed");
}
druid/examples/game_of_life.rs (line 427)
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
pub fn main() {
    let window = WindowDesc::new(make_widget())
        .window_size(Size {
            width: 800.0,
            height: 800.0,
        })
        .resizable(false)
        .title("Game of Life");
    let mut grid = Grid::new();
    let pattern0 = glider(GridPos { row: 5, col: 5 });
    if let Some(x) = pattern0 {
        grid.set_alive(&x);
    }
    let pattern1 = blinker(GridPos { row: 29, col: 29 });
    if let Some(x) = pattern1 {
        grid.set_alive(&x);
    }

    AppLauncher::with_window(window)
        .log_to_console()
        .launch(AppData {
            grid,
            drawing: false,
            paused: false,
            updates_per_second: 10.0,
        })
        .expect("launch failed");
}

Builder-style method to set whether this window’s titlebar is visible.

Examples found in repository
druid/examples/transparency.rs (line 33)
31
32
33
34
35
36
37
38
39
40
41
42
43
pub fn main() {
    let window = WindowDesc::new(build_root_widget())
        .show_titlebar(false)
        .window_size((512., 512.))
        .transparent(true)
        .resizable(true)
        .title("Transparent background");

    AppLauncher::with_window(window)
        .log_to_console()
        .launch(HelloState { name: "".into() })
        .expect("launch failed");
}

Builder-style method to set whether this window’s background should be transparent.

Examples found in repository
druid/examples/transparency.rs (line 35)
31
32
33
34
35
36
37
38
39
40
41
42
43
pub fn main() {
    let window = WindowDesc::new(build_root_widget())
        .show_titlebar(false)
        .window_size((512., 512.))
        .transparent(true)
        .resizable(true)
        .title("Transparent background");

    AppLauncher::with_window(window)
        .log_to_console()
        .launch(HelloState { name: "".into() })
        .expect("launch failed");
}

Sets the initial window position in display points, relative to the origin of the virtual screen.

Sets the WindowLevel of the window

Set initial state for the window.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

Performs the conversion.

Performs the conversion.

Should always be Self

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more