New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add bindings for calc() #12465
Add bindings for calc() #12465
Changes from all commits
8b76bd7
b52b78c
67bcb96
8fddc46
704d7a0
f51b115
File filter...
Jump to…
| @@ -0,0 +1,36 @@ | ||
| /* This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
|
||
| //! This module contains conversion helpers between Servo and Gecko types | ||
| //! Ideally, it would be in geckolib itself, but coherence | ||
| //! forces us to keep the traits and implementations here | ||
emilio
Member
|
||
|
|
||
| use app_units::Au; | ||
| use gecko_bindings::structs::nsStyleCoord_CalcValue; | ||
| use values::computed::CalcLengthOrPercentage; | ||
|
|
||
| impl From<CalcLengthOrPercentage> for nsStyleCoord_CalcValue { | ||
| fn from(other: CalcLengthOrPercentage) -> nsStyleCoord_CalcValue { | ||
| let has_percentage = other.percentage.is_some(); | ||
| nsStyleCoord_CalcValue { | ||
| mLength: other.length.map_or(0, |l| l.0), | ||
| mPercent: other.percentage.unwrap_or(0.0), | ||
| mHasPercent: has_percentage, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl From<nsStyleCoord_CalcValue> for CalcLengthOrPercentage { | ||
| fn from(other: nsStyleCoord_CalcValue) -> CalcLengthOrPercentage { | ||
| let percentage = if other.mHasPercent { | ||
| Some(other.mPercent) | ||
| } else { | ||
| None | ||
| }; | ||
| CalcLengthOrPercentage { | ||
| length: Some(Au(other.mLength)), | ||
| percentage: percentage, | ||
| } | ||
| } | ||
| } | ||
| @@ -0,0 +1,54 @@ | ||
| /* This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
|
||
| use bindings::{Gecko_ResetStyleCoord, Gecko_SetStyleCoordCalcValue, Gecko_AddRefCalcArbitraryThread}; | ||
| use std::mem::transmute; | ||
| use structs::{nsStyleCoord_CalcValue, nsStyleCoord_Calc, nsStyleUnit, nsStyleUnion, nsStyleCoord}; | ||
|
|
||
| // Functions here are unsafe because it is possible to use the wrong nsStyleUnit | ||
| // FIXME we should be pairing up nsStyleUnion and nsStyleUnit somehow | ||
| // nsStyleCoord is one way to do it, but there are other structs using pairs | ||
| // of union and unit too | ||
bholley
Contributor
|
||
|
|
||
| impl nsStyleUnion { | ||
| /// Clean up any resources used by an nsStyleUnit | ||
| /// Currently, this only happens if the nsStyleUnit | ||
| /// is a Calc | ||
| pub unsafe fn reset(&mut self, unit: &mut nsStyleUnit) { | ||
| if *unit == nsStyleUnit::eStyleUnit_Calc { | ||
| Gecko_ResetStyleCoord(unit, self); | ||
| } | ||
| } | ||
|
|
||
| /// Set internal value to a calc() value | ||
| /// reset() the union before calling this | ||
| pub unsafe fn set_calc_value(&mut self, unit: &mut nsStyleUnit, v: nsStyleCoord_CalcValue) { | ||
| // Calc should have been cleaned up | ||
| debug_assert!(*unit != nsStyleUnit::eStyleUnit_Calc); | ||
| Gecko_SetStyleCoordCalcValue(unit, self, v); | ||
| } | ||
|
|
||
| pub unsafe fn get_calc(&self) -> nsStyleCoord_CalcValue { | ||
| (*self.as_calc())._base | ||
| } | ||
|
|
||
| pub unsafe fn addref_if_calc(&mut self, unit: &nsStyleUnit) { | ||
| if *unit == nsStyleUnit::eStyleUnit_Calc { | ||
| Gecko_AddRefCalcArbitraryThread(self.as_calc_mut()); | ||
| } | ||
| } | ||
|
|
||
| unsafe fn as_calc_mut(&mut self) -> &mut nsStyleCoord_Calc { | ||
| transmute(*self.mPointer.as_mut() as *mut nsStyleCoord_Calc) | ||
| } | ||
| unsafe fn as_calc(&self) -> &nsStyleCoord_Calc { | ||
| transmute(*self.mPointer.as_ref() as *const nsStyleCoord_Calc) | ||
| } | ||
| } | ||
|
|
||
| impl nsStyleCoord { | ||
| pub unsafe fn addref_if_calc(&mut self) { | ||
| self.mValue.addref_if_calc(&self.mUnit); | ||
| } | ||
| } | ||
Question: all uses of these two
Fromimpls are going to be in the geckolib crate, so why can't they live in ports/geckolib/values.rs?