vanilladocs
vanillapunch.com
API / ui/autolayout/layoutvariable

ui/autolayout/layoutvariable

레이아웃 솔버 미지수. (원본 ui/autolayout/layoutvariable.js 의 LayoutVariable)

  • 사용자 코드가 식에 쓰는 변수다. 솔버가 풀고 나서 update_variables 를 부르면 set_value 로 결과가 들어온다.
  • 원본과 다른 것:
  • 원본은 객체 하나를 여러 곳(노드, 식, 솔버)이 같이 가리킨다. 러스트 판은 Rc 를 품은 값싼 Clone 핸들이라 복제한 핸들끼리 같은 이름과 값을 본다. 같음과 해시는 원본의 객체 동일성처럼 핸들이 가리키는 변수로 가린다.
  • 원본에 없는 get_id 로 변수마다 붙는 고유 번호를 읽는다.
  • 이름과 값은 RefCell, Cell 에 두어 &self 로 바꾼다.

Layout solver unknown. (LayoutVariable of the original ui/autolayout/layoutvariable.js)

  • A variable user code uses in expressions. After the solver solves, calling update_variables puts the result in via set_value.
  • Differences from the original:
  • In the original, several places (nodes, expressions, the solver) point to one object. The Rust version is a cheap Clone handle holding an Rc, so cloned handles see the same name and value. Equality and hashing go by the variable the handle points to, like the original's object identity.
  • get_id, which the original lacks, reads the unique number assigned to each variable.
  • The name and value are kept in RefCell and Cell so they change through &self.

LayoutVariableEventtype

소스source

값이 바뀐 변수를 받는 함수. (원본 setValueChangedEvent 의 함수)

Function that receives the changed variable. (the function of the original setValueChangedEvent)

pub type LayoutVariableEvent = Rc<dyn Fn(&LayoutVariable)>

LayoutVariablestruct traits Clone, PartialEq, Eq, Hash, fmt::Debug

짝Counterpart: vanilla.js LayoutVariable소스source

레이아웃 솔버 미지수. (복제해도 같은 변수를 가리킨다)

Layout solver unknown. (clones point to the same variable)

예제Example솔버가 푼 값을 읽기, 복제한 핸들도 같은 값을 본다Reading the value the solver solved, cloned handles see the same value
use vanilla::{LayoutExpression, LayoutSolver, LayoutVariable};

let solver = LayoutSolver::new();
let left = LayoutVariable::new("left");
let width = LayoutVariable::new("width");
let right = LayoutVariable::new("right");
solver.add_constraint(&LayoutExpression::from_variable(&left).equal_to(16)).unwrap();
solver.add_constraint(&LayoutExpression::from_variable(&width).equal_to(200)).unwrap();
solver.add_constraint(&LayoutExpression::from_variable(&right).equal_to(LayoutExpression::from_variable(&left).add(&width))).unwrap();
solver.update_variables();
assert!((right.get_value() - 216.0).abs() < 1e-9);

let shared = right.clone();
assert_eq!(shared, right);
assert_eq!(shared.get_id(), right.get_id());
assert_eq!(shared.get_value(), right.get_value());
생성자constructorpub fn new(name: &str) -> LayoutVariable#0.4.0 부터since 0.4.0소스source

생성. (값은 0)

Constructor. (value is 0)

name&str
이름. (보이기용, 이름이 같아도 따로 만든 변수는 다르다)Name. (for display, variables created separately are different even with the same name)

반환ReturnsLayoutVariable 새 고유 번호를 붙인 새 변수 핸들.A new variable handle with a new unique number.

짝Counterpart: vanilla.js LayoutVariable.constructor

메서드methodpub fn get_id(&self) -> u64#0.4.0 부터since 0.4.0소스source

고유 번호 반환.

Returns the unique number.

반환Returnsu64 고유 번호. (1 부터 만든 차례로 하나씩 늘어난다, 복제한 핸들은 같은 번호)Unique number. (increases by one in creation order from 1, cloned handles have the same number)

메서드methodpub fn set_name(&self, name: &str)#0.4.0 부터since 0.4.0소스source

이름 설정.

Sets the name.

name&str
새 이름. (복제한 핸들 모두에 보인다)New name. (visible to all cloned handles)

짝Counterpart: vanilla.js LayoutVariable.setName

메서드methodpub fn get_name(&self) -> String#0.4.0 부터since 0.4.0소스source

이름 반환.

Returns the name.

반환ReturnsString 이름의 사본.Copy of the name.

짝Counterpart: vanilla.js LayoutVariable.getName

메서드methodpub fn set_value(&self, value: f64)#0.4.0 부터since 0.4.0소스source

값 설정. (솔버가 부르며 바깥에서 직접 부르는 것은 권하지 않는다)

Sets the value. (called by the solver, calling it directly from outside is not recommended)

valuef64
넣을 값.Value to set.

짝Counterpart: vanilla.js LayoutVariable.setValue

메서드methodpub fn set_value_changed_event(&self, value_changed_event: Option<LayoutVariableEvent>)#0.4.0 부터since 0.4.0소스source

값이 바뀔 때 부를 함수 설정. (원본 setValueChangedEvent, 솔버가 푼 값이 달라질 때만 부른다, None 이면 부르지 않는다)

  • 변수를 가진 쪽이 바뀐 것을 알아챈다. (원본 UINode 는 자리와 크기 변수에 걸어 하위 트리 그리기 경계를 다시 셈하게 한다. 러스트 판 UINode 는 노드 트랜스폼에 풀어 넣으므로 트랜스폼 세터가 알린다)

Sets the function to call when the value changes. (original setValueChangedEvent, called only when the value solved by the solver differs, not called if None)

  • Lets the owner of the variable notice the change. (the original UINode sets it on its position and size variables to recompute subtree draw bounds. The Rust UINode writes the solution into the node transform, so the transform setters report it)
value_changed_eventOption<LayoutVariableEvent>
바뀐 변수를 받는 함수. None 이면 지운다.Function that receives the changed variable. None clears it.

짝Counterpart: vanilla.js LayoutVariable.setValueChangedEvent

예제Example값이 바뀔 때 알기Notice when the value changes
use std::cell::Cell;
use std::rc::Rc;
use vanilla::LayoutVariable;

let width = LayoutVariable::new("width");
let changed_count = Rc::new(Cell::new(0));
let counter = changed_count.clone();
width.set_value_changed_event(Some(Rc::new(move |variable: &LayoutVariable| {
	assert_eq!(variable.get_value(), 120.0);
	counter.set(counter.get() + 1);
})));
width.set_value(120.0);
width.set_value(120.0);
assert_eq!(changed_count.get(), 1);
assert!(width.get_value_changed_event().is_some());
메서드methodpub fn get_value_changed_event(&self) -> Option<LayoutVariableEvent>#0.4.0 부터since 0.4.0소스source

값이 바뀔 때 부를 함수 반환. (원본 getValueChangedEvent)

Returns the function called when the value changes. (original getValueChangedEvent)

반환ReturnsOption<LayoutVariableEvent> 건 함수. 없으면 None.The set function. None if there is none.

짝Counterpart: vanilla.js LayoutVariable.getValueChangedEvent

메서드methodpub fn get_value(&self) -> f64#0.4.0 부터since 0.4.0소스source

현재 값 반환. (update_variables 뒤에 맞다)

Returns the current value. (correct after update_variables)

반환Returnsf64 지금 값. (처음은 0)Current value. (initially 0)

짝Counterpart: vanilla.js LayoutVariable.getValue