Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting minimum window size #701

Merged
merged 4 commits into from
Mar 20, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions druid-shell/src/platform/gtk/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ pub struct WindowBuilder {
title: String,
menu: Option<Menu>,
size: Size,
min_size: Option<Size>,
resizable: bool,
show_titlebar: bool,
}
Expand Down Expand Up @@ -116,6 +117,7 @@ impl WindowBuilder {
title: String::new(),
menu: None,
size: Size::new(500.0, 400.0),
min_size: None,
resizable: true,
show_titlebar: true,
}
Expand All @@ -129,6 +131,10 @@ impl WindowBuilder {
self.size = size;
}

pub fn set_min_size(&mut self, size: Size) {
self.min_size = Some(size);
}

pub fn resizable(&mut self, resizable: bool) {
self.resizable = resizable;
}
Expand Down Expand Up @@ -224,6 +230,13 @@ impl WindowBuilder {
Inhibit(true)
});

if let Some(min_size) = self.min_size {
drawing_area.set_size_request(
(min_size.width * dpi_scale) as i32,
(min_size.height * dpi_scale) as i32,
);
}

let last_size = Cell::new((0, 0));

drawing_area.connect_draw(clone!(handle => move |widget, context| {
Expand Down
7 changes: 7 additions & 0 deletions druid-shell/src/platform/mac/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ pub(crate) struct WindowBuilder {
title: String,
menu: Option<Menu>,
size: Size,
min_size: Option<Size>,
resizable: bool,
show_titlebar: bool,
}
Expand Down Expand Up @@ -107,6 +108,7 @@ impl WindowBuilder {
title: String::new(),
menu: None,
size: Size::new(500.0, 400.0),
min_size: None,
resizable: true,
show_titlebar: true,
}
Expand All @@ -120,6 +122,11 @@ impl WindowBuilder {
self.size = size;
}

pub fn set_min_size(&mut self, size: Size) {
// TODO: Use this in `self.build`
self.min_size = Some(size);
}

pub fn resizable(&mut self, resizable: bool) {
// TODO: Use this in `self.build`
self.resizable = resizable;
Expand Down
7 changes: 7 additions & 0 deletions druid-shell/src/platform/windows/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ pub struct WindowBuilder {
resizable: bool,
show_titlebar: bool,
size: Size,
min_size: Option<Size>,
}

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
Expand Down Expand Up @@ -706,6 +707,7 @@ impl WindowBuilder {
show_titlebar: true,
present_strategy: Default::default(),
size: Size::new(500.0, 400.0),
min_size: None,
}
}

Expand All @@ -718,6 +720,11 @@ impl WindowBuilder {
self.size = size;
}

pub fn set_min_size(&mut self, size: Size) {
// TODO: Use this in `self.build`
self.min_size = Some(size);
}

pub fn resizable(&mut self, resizable: bool) {
// TODO: Use this in `self.build`
self.resizable = resizable;
Expand Down
5 changes: 5 additions & 0 deletions druid-shell/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ impl WindowBuilder {
self.0.set_size(size)
}

/// Set the window's initial size.
pub fn set_min_size(&mut self, size: Size) {
self.0.set_min_size(size)
}

/// Set whether the window should be resizable
pub fn resizable(&mut self, resizable: bool) {
self.0.resizable(resizable)
Expand Down
3 changes: 2 additions & 1 deletion druid/examples/flex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,8 @@ fn make_ui() -> impl Widget<AppState> {

fn main() -> Result<(), PlatformError> {
let main_window = WindowDesc::new(make_ui)
.window_size((600., 600.00))
.window_size((620., 265.00))
cmyr marked this conversation as resolved.
Show resolved Hide resolved
.with_min_size((620., 265.00))
.title(LocalizedString::new("Flex Container Options"));

let demo_state = DemoState {
Expand Down
13 changes: 13 additions & 0 deletions druid/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub struct WindowDesc<T> {
pub(crate) root: Box<dyn Widget<T>>,
pub(crate) title: LocalizedString<T>,
pub(crate) size: Option<Size>,
pub(crate) min_size: Option<Size>,
pub(crate) menu: Option<MenuDesc<T>>,
pub(crate) resizable: bool,
pub(crate) show_titlebar: bool,
Expand Down Expand Up @@ -145,6 +146,7 @@ impl<T: Data> WindowDesc<T> {
root: root().boxed(),
title: LocalizedString::new("app-name"),
size: None,
min_size: None,
menu: MenuDesc::platform_default(),
resizable: true,
show_titlebar: true,
Expand Down Expand Up @@ -179,6 +181,14 @@ impl<T: Data> WindowDesc<T> {
self
}

/// Set the minimal window size, similar to [`window_size`]
cmyr marked this conversation as resolved.
Show resolved Hide resolved
///
/// [`window_size`]: struct.WindowDesc.html#method.window_size
pub fn with_min_size(mut self, size: impl Into<Size>) -> Self {
self.min_size = Some(size.into());
self
}

pub fn resizable(mut self, resizable: bool) -> Self {
self.resizable = resizable;
self
Expand Down Expand Up @@ -211,6 +221,9 @@ impl<T: Data> WindowDesc<T> {
if let Some(size) = self.size {
builder.set_size(size);
}
if let Some(min_size) = self.min_size {
builder.set_min_size(min_size);
}

builder.set_title(self.title.localized_str());
if let Some(menu) = platform_menu {
Expand Down