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 {
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();