Skip to content

Commit

Permalink
[GTK] Implement content_insets for GTK. (#1722)
Browse files Browse the repository at this point in the history
Implement content_insets for GTK.
  • Loading branch information
jneem authored Apr 28, 2021
1 parent a73120c commit 04cb3e0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ You can find its changes [documented below](#070---2021-01-01).
context-methods to implement disabled ([#1632] by [@xarvic])
- `LifeCycle::BuildFocusChain` to update the focus-chain ([#1632] by [@xarvic])
- `DisabledIf` widget wrapper to disable based on the state of Data and Env ([#1702] by [@xarvic])
- GTK: added support for `content_insets` ([#1722] by [@jneem])

### Changed

- Warn on unhandled Commands ([#1533] by [@Maan2003])
Expand Down Expand Up @@ -685,6 +687,7 @@ Last release without a changelog :(
[#1702]: https://github.com/linebender/druid/pull/1702
[#1713]: https://github.com/linebender/druid/pull/1713
[#1715]: https://github.com/linebender/druid/pull/1715
[#1722]: https://github.com/linebender/druid/pull/1722
[#1724]: https://github.com/linebender/druid/pull/1724
[#1730]: https://github.com/linebender/druid/pull/1730

Expand Down
23 changes: 21 additions & 2 deletions druid-shell/src/platform/gtk/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -875,9 +875,28 @@ impl WindowHandle {
}
}

/// The GTK implementation of content_insets differs from, e.g., the Windows one in that it
/// doesn't try to account for window decorations. Depending on the platform, GTK might not
/// even be aware of the size of the window decorations. And anyway, GTK's `Window::resize`
/// function [tries not to include] the window decorations, so it makes sense not to include
/// them here either.
///
/// [tries not to include]: https://developer.gnome.org/gtk3/stable/GtkWidget.html#geometry-management
pub fn content_insets(&self) -> Insets {
warn!("WindowHandle::content_insets unimplemented for GTK platforms.");
Insets::ZERO
if let Some(state) = self.state.upgrade() {
let scale = state.scale.get();
let (width_px, height_px) = state.window.get_size();
let alloc_px = state.drawing_area.get_allocation();
let window = Size::new(width_px as f64, height_px as f64).to_dp(scale);
let alloc = Rect::from_origin_size(
(alloc_px.x as f64, alloc_px.y as f64),
(alloc_px.width as f64, alloc_px.height as f64),
)
.to_dp(scale);
window.to_rect() - alloc
} else {
Insets::ZERO
}
}

pub fn set_level(&self, level: WindowLevel) {
Expand Down
10 changes: 9 additions & 1 deletion druid-shell/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,15 @@ impl WindowHandle {

/// Returns the insets of the window content from its position and size in [display points].
///
/// This is to account for any window system provided chrome, eg. title bars.
/// This is to account for any window system provided chrome, e.g. title bars. For example, if
/// you want your window to have room for contents of size `contents`, then you should call
/// [`WindowHandle::get_size`] with an argument of `(contents.to_rect() + insets).size()`,
/// where `insets` is the return value of this function.
///
/// The details of this function are somewhat platform-dependent. For example, on Windows both
/// the insets and the window size include the space taken up by the title bar and window
/// decorations; on GTK neither the insets nor the window size include the title bar or window
/// decorations.
///
/// [display points]: crate::Scale
pub fn content_insets(&self) -> Insets {
Expand Down

0 comments on commit 04cb3e0

Please sign in to comment.