Struct druid::WindowDesc
source · pub struct WindowDesc<T> {
pub id: WindowId,
/* private fields */
}
Expand description
A description of a window to be instantiated.
This struct has builder methods to specify some window properties. Each of
these methods usually corresponds to a platform API call when constructing the
function. Except for title()
, they have no default values and the APIS
won’t be called if the method is not used.
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§
source§impl<T: Data> WindowDesc<T>
impl<T: Data> WindowDesc<T>
sourcepub fn new<W>(root: W) -> WindowDesc<T>where
W: Widget<T> + 'static,
pub fn new<W>(root: W) -> WindowDesc<T>where W: Widget<T> + 'static,
Create a new WindowDesc
, taking the root Widget
for this window.
Examples found in repository?
More examples
- examples/blocking_function.rs
- examples/panels.rs
- examples/svg.rs
- examples/anim.rs
- examples/scroll_colors.rs
- examples/view_switcher.rs
- examples/lens.rs
- examples/open_save.rs
- examples/switches.rs
- examples/transparency.rs
- examples/invalidation.rs
- examples/multiwin.rs
- examples/timer.rs
- examples/styled_text.rs
- examples/disabled.rs
- examples/z_stack.rs
- examples/list.rs
- examples/calc.rs
- examples/hello.rs
- examples/slider.rs
- examples/textbox.rs
- examples/markdown_preview.rs
- examples/sub_window.rs
- examples/tabs.rs
- examples/event_viewer.rs
- examples/game_of_life.rs
- examples/input_region.rs
- examples/flex.rs
- examples/async_event.rs
- examples/text.rs
sourcepub fn title(self, title: impl Into<LabelText<T>>) -> Self
pub fn title(self, title: impl Into<LabelText<T>>) -> Self
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.
If this method isn’t called, the default title will be LocalizedString::new("app-name")
.
Examples found in repository?
More examples
- examples/blocking_function.rs
- examples/panels.rs
- examples/svg.rs
- examples/anim.rs
- examples/scroll_colors.rs
- examples/view_switcher.rs
- examples/lens.rs
- examples/open_save.rs
- examples/switches.rs
- examples/transparency.rs
- examples/invalidation.rs
- examples/multiwin.rs
- examples/timer.rs
- examples/styled_text.rs
- examples/disabled.rs
- examples/z_stack.rs
- examples/list.rs
- examples/calc.rs
- examples/hello.rs
- examples/slider.rs
- examples/textbox.rs
- examples/markdown_preview.rs
- examples/sub_window.rs
- examples/tabs.rs
- examples/event_viewer.rs
- examples/game_of_life.rs
- examples/input_region.rs
- examples/flex.rs
- examples/async_event.rs
- examples/text.rs
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?
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
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");
}
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
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");
}
sourcepub fn window_size_policy(self, size_policy: WindowSizePolicy) -> Self
pub fn window_size_policy(self, size_policy: WindowSizePolicy) -> Self
Set the window size policy
sourcepub fn window_size(self, size: impl Into<Size>) -> Self
pub fn window_size(self, size: impl Into<Size>) -> Self
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?
48 49 50 51 52 53 54 55 56 57 58 59 60
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
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
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: State = State { counter: 0 };
// 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");
}
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
}
}
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");
}
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");
}
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
pub fn main() {
// describe the main window
let main_window = WindowDesc::new(build_root_widget())
.title("Slider Demo!")
.window_size((400.0, 400.0));
// create the initial app state
let initial_state: AppState = AppState {
range: (2.0, 8.0),
value: 5.0,
};
// 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");
}
sourcepub fn with_min_size(self, size: impl Into<Size>) -> Self
pub fn with_min_size(self, size: impl Into<Size>) -> Self
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?
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
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
fn main() {
let main_window = WindowDesc::new(InputRegionExampleWidget::new())
.title("Input Region Demo")
.window_size((750.0, 500.0))
.with_min_size((650.0, 450.0))
// Disable the titlebar since it breaks the desired effect on mac.
// It can be turned on with the button, but not on mac.
// A lot of apps that will use the interaction features will turn this off
// On Windows, if on, this will be invisible, but still there.
.show_titlebar(false)
.transparent(true);
let state = AppState {
limit_input_region: true,
always_on_top: false,
show_titlebar: false,
};
AppLauncher::with_window(main_window)
.log_to_console()
.launch(state)
.expect("Failed to launch application");
}
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 351
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");
}
sourcepub fn resizable(self, resizable: bool) -> Self
pub fn resizable(self, resizable: bool) -> Self
Builder-style method to set whether this window can be resized.
Examples found in repository?
48 49 50 51 52 53 54 55 56 57 58 59 60
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
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");
}
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");
}
sourcepub fn show_titlebar(self, show_titlebar: bool) -> Self
pub fn show_titlebar(self, show_titlebar: bool) -> Self
Builder-style method to set whether this window’s titlebar is visible.
Examples found in repository?
48 49 50 51 52 53 54 55 56 57 58 59 60
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
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
fn main() {
let main_window = WindowDesc::new(InputRegionExampleWidget::new())
.title("Input Region Demo")
.window_size((750.0, 500.0))
.with_min_size((650.0, 450.0))
// Disable the titlebar since it breaks the desired effect on mac.
// It can be turned on with the button, but not on mac.
// A lot of apps that will use the interaction features will turn this off
// On Windows, if on, this will be invisible, but still there.
.show_titlebar(false)
.transparent(true);
let state = AppState {
limit_input_region: true,
always_on_top: false,
show_titlebar: false,
};
AppLauncher::with_window(main_window)
.log_to_console()
.launch(state)
.expect("Failed to launch application");
}
sourcepub fn transparent(self, transparent: bool) -> Self
pub fn transparent(self, transparent: bool) -> Self
Builder-style method to set whether this window’s background should be transparent.
Examples found in repository?
48 49 50 51 52 53 54 55 56 57 58 59 60
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
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
fn main() {
let main_window = WindowDesc::new(InputRegionExampleWidget::new())
.title("Input Region Demo")
.window_size((750.0, 500.0))
.with_min_size((650.0, 450.0))
// Disable the titlebar since it breaks the desired effect on mac.
// It can be turned on with the button, but not on mac.
// A lot of apps that will use the interaction features will turn this off
// On Windows, if on, this will be invisible, but still there.
.show_titlebar(false)
.transparent(true);
let state = AppState {
limit_input_region: true,
always_on_top: false,
show_titlebar: false,
};
AppLauncher::with_window(main_window)
.log_to_console()
.launch(state)
.expect("Failed to launch application");
}
sourcepub fn set_position(self, position: impl Into<Point>) -> Self
pub fn set_position(self, position: impl Into<Point>) -> Self
Sets the initial window position in display points, relative to the origin of the virtual screen.
sourcepub fn set_level(self, level: WindowLevel) -> Self
pub fn set_level(self, level: WindowLevel) -> Self
Sets the WindowLevel
of the window
sourcepub fn set_always_on_top(self, always_on_top: bool) -> Self
pub fn set_always_on_top(self, always_on_top: bool) -> Self
Sets whether the window is always on top.
An always on top window stays on top, even after clicking off of it.
sourcepub fn set_window_state(self, state: WindowState) -> Self
pub fn set_window_state(self, state: WindowState) -> Self
Set initial state for the window.
sourcepub fn with_config(self, config: WindowConfig) -> Self
pub fn with_config(self, config: WindowConfig) -> Self
Set the WindowConfig
of window.