From bcd0b74838376564c6dded6ad142a6b632e31ff6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Tue, 6 Jun 2023 13:05:08 +0200 Subject: [PATCH] style: Honor GTK button layout This is based off work by smurfd. But this patch doesn't support buttons both at the left and right, which simplifies a lot the implementation. Also, clean-up the existing env variables while at it. Co-authored-by: Nicklas Boman Differential Revision: https://phabricator.services.mozilla.com/D132073 --- components/style/custom_properties.rs | 82 ++++++++++++++++++++----- components/style/gecko/media_queries.rs | 16 ----- 2 files changed, 67 insertions(+), 31 deletions(-) diff --git a/components/style/custom_properties.rs b/components/style/custom_properties.rs index 931bf32a00c..76688afcea1 100644 --- a/components/style/custom_properties.rs +++ b/components/style/custom_properties.rs @@ -49,19 +49,19 @@ macro_rules! make_variable { } fn get_safearea_inset_top(device: &Device) -> VariableValue { - VariableValue::pixel(device.safe_area_insets().top) + VariableValue::pixels(device.safe_area_insets().top) } fn get_safearea_inset_bottom(device: &Device) -> VariableValue { - VariableValue::pixel(device.safe_area_insets().bottom) + VariableValue::pixels(device.safe_area_insets().bottom) } fn get_safearea_inset_left(device: &Device) -> VariableValue { - VariableValue::pixel(device.safe_area_insets().left) + VariableValue::pixels(device.safe_area_insets().left) } fn get_safearea_inset_right(device: &Device) -> VariableValue { - VariableValue::pixel(device.safe_area_insets().right) + VariableValue::pixels(device.safe_area_insets().right) } static ENVIRONMENT_VARIABLES: [EnvironmentVariable; 4] = [ @@ -71,17 +71,47 @@ static ENVIRONMENT_VARIABLES: [EnvironmentVariable; 4] = [ make_variable!(atom!("safe-area-inset-right"), get_safearea_inset_right), ]; -fn get_titlebar_radius(device: &Device) -> VariableValue { - VariableValue::pixel(device.titlebar_radius()) +macro_rules! lnf_int { + ($id:ident) => { + unsafe { + crate::gecko_bindings::bindings::Gecko_GetLookAndFeelInt( + crate::gecko_bindings::bindings::LookAndFeel_IntID::$id as i32, + ) + } + }; } -fn get_menu_radius(device: &Device) -> VariableValue { - VariableValue::pixel(device.menu_radius()) +macro_rules! lnf_int_variable { + ($atom:expr, $id:ident, $ctor:ident) => {{ + fn __eval(_: &Device) -> VariableValue { + VariableValue::$ctor(lnf_int!($id)) + } + make_variable!($atom, __eval) + }}; } -static CHROME_ENVIRONMENT_VARIABLES: [EnvironmentVariable; 2] = [ - make_variable!(atom!("-moz-gtk-csd-titlebar-radius"), get_titlebar_radius), - make_variable!(atom!("-moz-gtk-menu-radius"), get_menu_radius), +static CHROME_ENVIRONMENT_VARIABLES: [EnvironmentVariable; 5] = [ + lnf_int_variable!( + atom!("-moz-gtk-csd-titlebar-radius"), + TitlebarRadius, + int_pixels + ), + lnf_int_variable!(atom!("-moz-gtk-csd-menu-radius"), GtkMenuRadius, int_pixels), + lnf_int_variable!( + atom!("-moz-gtk-csd-close-button-position"), + GTKCSDCloseButtonPosition, + integer + ), + lnf_int_variable!( + atom!("-moz-gtk-csd-minimize-button-position"), + GTKCSDMinimizeButtonPosition, + integer + ), + lnf_int_variable!( + atom!("-moz-gtk-csd-maximize-button-position"), + GTKCSDMaximizeButtonPosition, + integer + ), ]; impl CssEnvironment { @@ -280,17 +310,39 @@ impl VariableValue { })) } - /// Create VariableValue from css pixel value - pub fn pixel(number: f32) -> Self { + /// Create VariableValue from an int. + fn integer(number: i32) -> Self { + Self::from_token(Token::Number { + has_sign: false, + value: number as f32, + int_value: Some(number), + }) + } + + /// Create VariableValue from a float amount of CSS pixels. + fn pixels(number: f32) -> Self { // FIXME (https://github.com/servo/rust-cssparser/issues/266): // No way to get TokenSerializationType::Dimension without creating // Token object. - let token = Token::Dimension { + Self::from_token(Token::Dimension { has_sign: false, value: number, int_value: None, unit: CowRcStr::from("px"), - }; + }) + } + + /// Create VariableValue from an integer amount of CSS pixels. + fn int_pixels(number: i32) -> Self { + Self::from_token(Token::Dimension { + has_sign: false, + value: number as f32, + int_value: Some(number), + unit: CowRcStr::from("px"), + }) + } + + fn from_token(token: Token) -> Self { let token_type = token.serialization_type(); let mut css = token.to_css_string(); css.shrink_to_fit(); diff --git a/components/style/gecko/media_queries.rs b/components/style/gecko/media_queries.rs index 19ed6444f07..e4634d7eab9 100644 --- a/components/style/gecko/media_queries.rs +++ b/components/style/gecko/media_queries.rs @@ -459,22 +459,6 @@ impl Device { } } - /// Returns the gtk titlebar radius in CSS pixels. - pub fn titlebar_radius(&self) -> f32 { - unsafe { - bindings::Gecko_GetLookAndFeelInt(bindings::LookAndFeel_IntID::TitlebarRadius as i32) - as f32 - } - } - - /// Returns the gtk menu radius in CSS pixels. - pub fn menu_radius(&self) -> f32 { - unsafe { - bindings::Gecko_GetLookAndFeelInt(bindings::LookAndFeel_IntID::GtkMenuRadius as i32) - as f32 - } - } - /// Return whether the document is a chrome document. #[inline] pub fn is_chrome_document(&self) -> bool {