Skip to content

Commit

Permalink
feat(linux): add drawing behavior builder methods, closes #567 (#572)
Browse files Browse the repository at this point in the history
* feat(linux): add drawing behavior builder methods, closes #567

* changefile

* fix docs
  • Loading branch information
amrbashir committed Sep 27, 2022
1 parent 937aba7 commit 0637c60
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changes/painting-builder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tao": minor
---

Add builder methods on Linux to control the drawing behavior of the window. `WindowBuilderExtUnix::with_double_buffered`, `WindowBuilderExtUnix::with_rgba_visual` and `WindowBuilderExtUnix::with_app_paintable`
32 changes: 32 additions & 0 deletions src/platform/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,23 @@ pub trait WindowBuilderExtUnix {
/// For anyone who wants to draw the background themselves, set this to `false`.
/// Default is `true`.
fn with_transparent_draw(self, draw: bool) -> WindowBuilder;

/// Whether to enable or disable the double buffered rendering of the window.
///
/// Default is `true`.
fn with_double_buffered(self, double_buffered: bool) -> WindowBuilder;

/// Whether to enable the rgba visual for the window.
///
/// Default is `false` but is always `true` if [`WindowAttributes::transparent`](crate::window::WindowAttributes::transparent) is `true`
fn with_rgba_visual(self, rgba_visual: bool) -> WindowBuilder;

/// Wether to set this window as app paintable
///
/// <https://docs.gtk.org/gtk3/method.Widget.set_app_paintable.html>
///
/// Default is `false` but is always `true` if [`WindowAttributes::transparent`](crate::window::WindowAttributes::transparent) is `true`
fn with_app_paintable(self, app_paintable: bool) -> WindowBuilder;
}

impl WindowBuilderExtUnix for WindowBuilder {
Expand All @@ -74,6 +91,21 @@ impl WindowBuilderExtUnix for WindowBuilder {
self.platform_specific.auto_transparent = draw;
self
}

fn with_double_buffered(mut self, double_buffered: bool) -> WindowBuilder {
self.platform_specific.double_buffered = double_buffered;
self
}

fn with_rgba_visual(mut self, rgba_visual: bool) -> WindowBuilder {
self.platform_specific.rgba_visual = rgba_visual;
self
}

fn with_app_paintable(mut self, app_paintable: bool) -> WindowBuilder {
self.platform_specific.app_paintable = app_paintable;
self
}
}

/// Additional methods on `EventLoop` that are specific to Unix.
Expand Down
6 changes: 6 additions & 0 deletions src/platform_impl/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ pub struct PlatformSpecificWindowBuilderAttributes {
pub parent: Parent,
pub skip_taskbar: bool,
pub auto_transparent: bool,
pub double_buffered: bool,
pub app_paintable: bool,
pub rgba_visual: bool,
}

impl Default for PlatformSpecificWindowBuilderAttributes {
Expand All @@ -70,6 +73,9 @@ impl Default for PlatformSpecificWindowBuilderAttributes {
parent: Default::default(),
skip_taskbar: Default::default(),
auto_transparent: true,
double_buffered: true,
app_paintable: false,
rgba_visual: false,
}
}
}
Expand Down
29 changes: 18 additions & 11 deletions src/platform_impl/linux/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,20 +139,27 @@ impl Window {
}

// Set GDK Visual
if let Some(screen) = window.screen() {
if let Some(visual) = screen.rgba_visual() {
window.set_visual(Some(&visual));
if pl_attribs.rgba_visual || attributes.transparent {
if let Some(screen) = window.screen() {
if let Some(visual) = screen.rgba_visual() {
window.set_visual(Some(&visual));
}
}
}

// Set a few attributes to make the window can be painted.
// See Gtk drawing model for more info:
// https://docs.gtk.org/gtk3/drawing-model.html
window.set_app_paintable(true);
let widget = window.upcast_ref::<gtk::Widget>();
if !event_loop_window_target.is_wayland() {
unsafe {
gtk::ffi::gtk_widget_set_double_buffered(widget.to_glib_none().0, 0);
if pl_attribs.app_paintable || attributes.transparent {
// Set a few attributes to make the window can be painted.
// See Gtk drawing model for more info:
// https://docs.gtk.org/gtk3/drawing-model.html
window.set_app_paintable(true);
}

if !pl_attribs.double_buffered {
let widget = window.upcast_ref::<gtk::Widget>();
if !event_loop_window_target.is_wayland() {
unsafe {
gtk::ffi::gtk_widget_set_double_buffered(widget.to_glib_none().0, 0);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ impl Window {
/// ## Platform-specific
///
/// - **iOS / Android / Linux:** Unsupported.
pub fn set_content_protection(&self, enabled: bool) {
pub fn set_content_protection(&self, #[allow(unused)] enabled: bool) {
#[cfg(any(target_os = "macos", target_os = "windows"))]
self.window.set_content_protection(enabled);
}
Expand Down

0 comments on commit 0637c60

Please sign in to comment.