Add Window::set_theme (#2553)

* Add `Window::set_theme`

* typo

* fix linux build

* fix wayland

* review changes

* update docs

* update changelog

* pin `image` dep

* suppport falling back to system default

* fix linux

* default to dark on macOS and x11

* fix `setAppearance` definition

* add macOS notes

* update docs

* Update CHANGELOG.md

Co-authored-by: Markus Siglreithmaier <m.siglreith@gmail.com>

* update doc

* Revert "pin `image` dep"

This reverts commit 7517f7c5065b4089ca146ce8799dab445ec32068.

* Update theme example with Window::set_theme

* Fix Window::theme getter on macOS

Co-authored-by: Markus Siglreithmaier <m.siglreith@gmail.com>
Co-authored-by: Mads Marquart <mads@marquart.dk>
This commit is contained in:
Amr Bashir 2022-11-29 11:05:51 +02:00 committed by GitHub
parent 9ae7498a8a
commit 28e34c2e1b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 127 additions and 65 deletions

View file

@ -100,8 +100,6 @@ pub struct PlatformSpecificWindowBuilderAttributes {
pub override_redirect: bool,
#[cfg(feature = "x11")]
pub x11_window_types: Vec<XWindowType>,
#[cfg(feature = "x11")]
pub gtk_theme_variant: Option<String>,
}
impl Default for PlatformSpecificWindowBuilderAttributes {
@ -120,8 +118,6 @@ impl Default for PlatformSpecificWindowBuilderAttributes {
override_redirect: false,
#[cfg(feature = "x11")]
x11_window_types: vec![XWindowType::Normal],
#[cfg(feature = "x11")]
gtk_theme_variant: None,
}
}
}
@ -585,6 +581,11 @@ impl Window {
x11_or_wayland!(match self; Window(window) => window.raw_display_handle())
}
#[inline]
pub fn set_theme(&self, theme: Option<Theme>) {
x11_or_wayland!(match self; Window(window) => window.set_theme(theme))
}
#[inline]
pub fn theme(&self) -> Option<Theme> {
x11_or_wayland!(match self; Window(window) => window.theme())

View file

@ -439,11 +439,6 @@ impl Window {
self.decorated.load(Ordering::Relaxed)
}
#[inline]
pub fn set_csd_theme(&self, theme: Theme) {
self.send_request(WindowRequest::CsdThemeVariant(theme));
}
#[inline]
pub fn set_minimized(&self, minimized: bool) {
// You can't unminimize the window on Wayland.
@ -620,6 +615,11 @@ impl Window {
self.event_loop_awakener.ping();
}
#[inline]
pub fn set_theme(&self, theme: Option<Theme>) {
self.send_request(WindowRequest::Theme(theme));
}
#[inline]
pub fn theme(&self) -> Option<Theme> {
None

View file

@ -59,9 +59,6 @@ pub enum WindowRequest {
/// Request decorations change.
Decorate(bool),
/// Request decorations change.
CsdThemeVariant(Theme),
/// Make the window resizeable.
Resizeable(bool),
@ -96,6 +93,9 @@ pub enum WindowRequest {
/// Window should be closed.
Close,
/// Change window theme.
Theme(Option<Theme>),
}
// The window update comming from the compositor.
@ -464,15 +464,6 @@ pub fn handle_window_requests(winit_state: &mut WinitState) {
let window_request = window_user_requests.get_mut(window_id).unwrap();
window_request.refresh_frame = true;
}
#[cfg(feature = "sctk-adwaita")]
WindowRequest::CsdThemeVariant(theme) => {
window_handle.window.set_frame_config(theme.into());
let window_requst = window_user_requests.get_mut(window_id).unwrap();
window_requst.refresh_frame = true;
}
#[cfg(not(feature = "sctk-adwaita"))]
WindowRequest::CsdThemeVariant(_) => {}
WindowRequest::Resizeable(resizeable) => {
window_handle.window.set_resizable(resizeable);
@ -537,6 +528,18 @@ pub fn handle_window_requests(winit_state: &mut WinitState) {
let event_sink = &mut winit_state.event_sink;
event_sink.push_window_event(WindowEvent::Destroyed, *window_id);
}
WindowRequest::Theme(_theme) => {
#[cfg(feature = "sctk-adwaita")]
{
window_handle.window.set_frame_config(match _theme {
Some(theme) => theme.into(),
None => sctk_adwaita::FrameConfig::auto(),
});
let window_requst = window_user_requests.get_mut(window_id).unwrap();
window_requst.refresh_frame = true;
}
}
};
}
}

View file

@ -301,6 +301,10 @@ impl UnownedWindow {
.set_decorations_inner(window_attrs.decorations)
.queue();
if let Some(theme) = window_attrs.preferred_theme {
window.set_theme_inner(Some(theme)).queue();
}
{
// Enable drag and drop (TODO: extend API to make this toggleable)
unsafe {
@ -359,10 +363,6 @@ impl UnownedWindow {
window.set_window_types(pl_attribs.x11_window_types).queue();
if let Some(variant) = pl_attribs.gtk_theme_variant {
window.set_gtk_theme_variant(variant).queue();
}
// set size hints
{
let mut min_inner_size = window_attrs
@ -565,9 +565,14 @@ impl UnownedWindow {
)
}
fn set_gtk_theme_variant(&self, variant: String) -> util::Flusher<'_> {
pub fn set_theme_inner(&self, theme: Option<Theme>) -> util::Flusher<'_> {
let hint_atom = unsafe { self.xconn.get_atom_unchecked(b"_GTK_THEME_VARIANT\0") };
let utf8_atom = unsafe { self.xconn.get_atom_unchecked(b"UTF8_STRING\0") };
let variant = match theme {
Some(Theme::Dark) => "dark",
Some(Theme::Light) => "light",
None => "dark",
};
let variant = CString::new(variant).expect("`_GTK_THEME_VARIANT` contained null byte");
self.xconn.change_property(
self.xwindow,
@ -578,6 +583,13 @@ impl UnownedWindow {
)
}
#[inline]
pub fn set_theme(&self, theme: Option<Theme>) {
self.set_theme_inner(theme)
.flush()
.expect("Failed to change window theme")
}
fn set_netwm(
&self,
operation: util::StateOperation,