From c91dfdd6fe8763811f72d6a8a268d10d29b3dc77 Mon Sep 17 00:00:00 2001 From: trimental Date: Tue, 29 Jan 2019 18:04:15 +0800 Subject: [PATCH] =?UTF-8?q?Wayland:=20add=20`set=5Fwayland=5Ftheme()`=20to?= =?UTF-8?q?=20control=20client=20decoration=20color=E2=80=A6=20(#775)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Wayland: add `set_wayland_theme()` to control client decoration color theme * Change &mut self to &self * Remove endianness comment --- CHANGELOG.md | 1 + src/os/unix.rs | 81 ++++++++++++++++++++++++++++ src/platform/linux/wayland/window.rs | 7 ++- 3 files changed, 88 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 13371a47..8dc7aa9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # Unreleased +- On Wayland, add `set_wayland_theme()` to control client decoration color theme - Added serde serialization to `os::unix::XWindowType`. - **Breaking:** `image` crate upgraded to 0.21. This is exposed as part of the `icon_loading` API. diff --git a/src/os/unix.rs b/src/os/unix.rs index 3d1525b3..3a468bc2 100644 --- a/src/os/unix.rs +++ b/src/os/unix.rs @@ -4,6 +4,8 @@ use std::os::raw; use std::ptr; use std::sync::Arc; +use sctk::window::{ButtonState, Theme}; + use { EventsLoop, LogicalSize, @@ -25,6 +27,74 @@ pub use platform::x11; pub use platform::XNotSupported; pub use platform::x11::util::WindowType as XWindowType; +/// Theme for wayland client side decorations +/// +/// Colors must be in ARGB8888 format +pub struct WaylandTheme { + /// Primary color when the window is focused + pub primary_active: [u8; 4], + /// Primary color when the window is unfocused + pub primary_inactive: [u8; 4], + /// Secondary color when the window is focused + pub secondary_active: [u8; 4], + /// Secondary color when the window is unfocused + pub secondary_inactive: [u8; 4], + /// Close button color when hovered over + pub close_button_hovered: [u8; 4], + /// Close button color + pub close_button: [u8; 4], + /// Close button color when hovered over + pub maximize_button_hovered: [u8; 4], + /// Maximize button color + pub maximize_button: [u8; 4], + /// Minimize button color when hovered over + pub minimize_button_hovered: [u8; 4], + /// Minimize button color + pub minimize_button: [u8; 4], +} + +struct WaylandThemeObject(WaylandTheme); + +impl Theme for WaylandThemeObject { + fn get_primary_color(&self, active: bool) -> [u8; 4] { + if active { + self.0.primary_active + } else { + self.0.primary_inactive + } + } + + // Used for division line + fn get_secondary_color(&self, active: bool) -> [u8; 4] { + if active { + self.0.secondary_active + } else { + self.0.secondary_inactive + } + } + + fn get_close_button_color(&self, state: ButtonState) -> [u8; 4] { + match state { + ButtonState::Hovered => self.0.close_button_hovered, + _ => self.0.close_button, + } + } + + fn get_maximize_button_color(&self, state: ButtonState) -> [u8; 4] { + match state { + ButtonState::Hovered => self.0.maximize_button_hovered, + _ => self.0.maximize_button, + } + } + + fn get_minimize_button_color(&self, state: ButtonState) -> [u8; 4] { + match state { + ButtonState::Hovered => self.0.minimize_button_hovered, + _ => self.0.minimize_button, + } + } +} + /// Additional methods on `EventsLoop` that are specific to Linux. pub trait EventsLoopExt { /// Builds a new `EventsLoop` that is forced to use X11. @@ -127,6 +197,9 @@ pub trait WindowExt { /// The pointer will become invalid when the glutin `Window` is destroyed. fn get_wayland_display(&self) -> Option<*mut raw::c_void>; + /// Sets the color theme of the client side window decorations on wayland + fn set_wayland_theme(&self, theme: WaylandTheme); + /// Check if the window is ready for drawing /// /// It is a remnant of a previous implementation detail for the @@ -202,6 +275,14 @@ impl WindowExt for Window { } } + #[inline] + fn set_wayland_theme(&self, theme: WaylandTheme) { + match self.window { + LinuxWindow::Wayland(ref w) => w.set_theme(WaylandThemeObject(theme)), + _ => {} + } + } + #[inline] fn is_ready(&self) -> bool { true diff --git a/src/platform/linux/wayland/window.rs b/src/platform/linux/wayland/window.rs index 62a74bef..27698631 100644 --- a/src/platform/linux/wayland/window.rs +++ b/src/platform/linux/wayland/window.rs @@ -7,7 +7,7 @@ use platform::{MonitorId as PlatformMonitorId, PlatformSpecificWindowBuilderAttr use window::MonitorId as RootMonitorId; use sctk::surface::{get_dpi_factor, get_outputs}; -use sctk::window::{ConceptFrame, Event as WEvent, Window as SWindow}; +use sctk::window::{ConceptFrame, Event as WEvent, Window as SWindow, Theme}; use sctk::reexports::client::{Display, Proxy}; use sctk::reexports::client::protocol::{wl_seat, wl_surface}; use sctk::reexports::client::protocol::wl_surface::RequestsTrait as SurfaceRequests; @@ -235,6 +235,11 @@ impl Window { } } + + pub fn set_theme(&self, theme: T) { + self.frame.lock().unwrap().set_theme(theme) + } + #[inline] pub fn set_cursor(&self, _cursor: MouseCursor) { // TODO