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 <smurfd@gmail.com>

Differential Revision: https://phabricator.services.mozilla.com/D132073
This commit is contained in:
Emilio Cobos Álvarez 2023-06-06 13:05:08 +02:00 committed by Oriol Brufau
parent 09a0edefb8
commit bcd0b74838
2 changed files with 67 additions and 31 deletions

View file

@ -49,19 +49,19 @@ macro_rules! make_variable {
} }
fn get_safearea_inset_top(device: &Device) -> VariableValue { 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 { 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 { 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 { 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] = [ 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), make_variable!(atom!("safe-area-inset-right"), get_safearea_inset_right),
]; ];
fn get_titlebar_radius(device: &Device) -> VariableValue { macro_rules! lnf_int {
VariableValue::pixel(device.titlebar_radius()) ($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 { macro_rules! lnf_int_variable {
VariableValue::pixel(device.menu_radius()) ($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] = [ static CHROME_ENVIRONMENT_VARIABLES: [EnvironmentVariable; 5] = [
make_variable!(atom!("-moz-gtk-csd-titlebar-radius"), get_titlebar_radius), lnf_int_variable!(
make_variable!(atom!("-moz-gtk-menu-radius"), get_menu_radius), 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 { impl CssEnvironment {
@ -280,17 +310,39 @@ impl VariableValue {
})) }))
} }
/// Create VariableValue from css pixel value /// Create VariableValue from an int.
pub fn pixel(number: f32) -> Self { 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): // FIXME (https://github.com/servo/rust-cssparser/issues/266):
// No way to get TokenSerializationType::Dimension without creating // No way to get TokenSerializationType::Dimension without creating
// Token object. // Token object.
let token = Token::Dimension { Self::from_token(Token::Dimension {
has_sign: false, has_sign: false,
value: number, value: number,
int_value: None, int_value: None,
unit: CowRcStr::from("px"), 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 token_type = token.serialization_type();
let mut css = token.to_css_string(); let mut css = token.to_css_string();
css.shrink_to_fit(); css.shrink_to_fit();

View file

@ -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. /// Return whether the document is a chrome document.
#[inline] #[inline]
pub fn is_chrome_document(&self) -> bool { pub fn is_chrome_document(&self) -> bool {