|
@@ -21,8 +21,8 @@ enum ButtonState {
|
|
}
|
|
}
|
|
|
|
|
|
pub struct Button<C: Component> {
|
|
pub struct Button<C: Component> {
|
|
- layout_root: LayoutNode,
|
|
|
|
- layout_label: LeafLayoutNode,
|
|
|
|
|
|
+ root_layout: LayoutNode,
|
|
|
|
+ label_layout: LeafLayoutNode,
|
|
text_cache: TextCache,
|
|
text_cache: TextCache,
|
|
label: String,
|
|
label: String,
|
|
state: ButtonState,
|
|
state: ButtonState,
|
|
@@ -45,8 +45,17 @@ impl<C: Component> Button<C> {
|
|
.set_halign(HorizontalAlignment::Centre);
|
|
.set_halign(HorizontalAlignment::Centre);
|
|
*layout.margin_mut() = PixelSideOffsets::new_all_same(uih.theme().border_width as i32);
|
|
*layout.margin_mut() = PixelSideOffsets::new_all_same(uih.theme().border_width as i32);
|
|
Self {
|
|
Self {
|
|
- layout_root: layout,
|
|
|
|
- layout_label: uih.new_layout_node().into(),
|
|
|
|
|
|
+ root_layout: layout,
|
|
|
|
+ label_layout: {
|
|
|
|
+ let mut node = uih.new_layout_node();
|
|
|
|
+ node
|
|
|
|
+ .set_width_policy(SizePolicy::fixed(0))
|
|
|
|
+ .set_height_policy(SizePolicy::fixed(0))
|
|
|
|
+ .set_halign(HorizontalAlignment::Centre)
|
|
|
|
+ .set_valign(VerticalAlignment::Centre);
|
|
|
|
+
|
|
|
|
+ node.into()
|
|
|
|
+ },
|
|
text_cache: TextCache::new_with_font_and_size(
|
|
text_cache: TextCache::new_with_font_and_size(
|
|
uih.theme().ui_font.clone(),
|
|
uih.theme().ui_font.clone(),
|
|
uih.theme().ui_font_size,
|
|
uih.theme().ui_font_size,
|
|
@@ -75,7 +84,7 @@ impl<C: Component> Button<C> {
|
|
|
|
|
|
impl<C: Component> LayoutTreeNode<RootTag> for Button<C> {
|
|
impl<C: Component> LayoutTreeNode<RootTag> for Button<C> {
|
|
fn current_node(&self) -> &LayoutNode {
|
|
fn current_node(&self) -> &LayoutNode {
|
|
- &self.layout_root
|
|
|
|
|
|
+ &self.root_layout
|
|
}
|
|
}
|
|
|
|
|
|
fn child_count(&self) -> usize {
|
|
fn child_count(&self) -> usize {
|
|
@@ -84,35 +93,24 @@ impl<C: Component> LayoutTreeNode<RootTag> for Button<C> {
|
|
|
|
|
|
fn child(&self, ndx: usize) -> Option<LayoutNodeAccess<'_>> {
|
|
fn child(&self, ndx: usize) -> Option<LayoutNodeAccess<'_>> {
|
|
if ndx == 0 {
|
|
if ndx == 0 {
|
|
- Some(self.layout_label.access())
|
|
|
|
|
|
+ Some(self.label_layout.access())
|
|
} else {
|
|
} else {
|
|
None
|
|
None
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-/*
|
|
|
|
-impl<C: Component> LayoutNodeContainer for Button<C> {
|
|
|
|
- fn layout_node(&self) -> &LayoutNode {
|
|
|
|
- &self.layout
|
|
|
|
- }
|
|
|
|
- fn layout_child(&self, ndx: usize) -> Option<LayoutNodeAccess<'_>> {
|
|
|
|
- None
|
|
|
|
- }
|
|
|
|
- fn layout_child_count(&self) -> usize {
|
|
|
|
- 0
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-*/
|
|
|
|
-
|
|
|
|
impl<C: Component> Widget<C> for Button<C> {
|
|
impl<C: Component> Widget<C> for Button<C> {
|
|
fn poll(
|
|
fn poll(
|
|
&mut self,
|
|
&mut self,
|
|
_uih: &mut UIHandle,
|
|
_uih: &mut UIHandle,
|
|
input_state: Option<&crate::input::InputState>,
|
|
input_state: Option<&crate::input::InputState>,
|
|
) -> Vec<<C as Component>::Msg> {
|
|
) -> Vec<<C as Component>::Msg> {
|
|
|
|
+
|
|
|
|
+ self.text_cache.update_node_size_hint(&mut *self.label_layout);
|
|
|
|
+
|
|
let mut result: Vec<<C as Component>::Msg> = vec![];
|
|
let mut result: Vec<<C as Component>::Msg> = vec![];
|
|
- if let Some((istate, area)) = input_state.zip(self.layout_root.render_area()) {
|
|
|
|
|
|
+ if let Some((istate, area)) = input_state.zip(self.root_layout.render_area()) {
|
|
let saved = self.state;
|
|
let saved = self.state;
|
|
match istate.mouse_status_in(area, MouseButton::Left) {
|
|
match istate.mouse_status_in(area, MouseButton::Left) {
|
|
MouseCheckStatus::Idle | MouseCheckStatus::Leave => {
|
|
MouseCheckStatus::Idle | MouseCheckStatus::Leave => {
|
|
@@ -130,12 +128,10 @@ impl<C: Component> Widget<C> for Button<C> {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if self.state != saved {
|
|
if self.state != saved {
|
|
- self.layout_root.render_needed();
|
|
|
|
|
|
+ self.root_layout.render_needed();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- // self.text_cache.update(&mut self.layout, &self.label);
|
|
|
|
-
|
|
|
|
result
|
|
result
|
|
}
|
|
}
|
|
|
|
|
|
@@ -143,18 +139,20 @@ impl<C: Component> Widget<C> for Button<C> {
|
|
LayoutNodeAccess::new(self)
|
|
LayoutNodeAccess::new(self)
|
|
}
|
|
}
|
|
fn layout_node_mut(&mut self) -> &mut LayoutNode {
|
|
fn layout_node_mut(&mut self) -> &mut LayoutNode {
|
|
- &mut self.layout_root
|
|
|
|
|
|
+ &mut self.root_layout
|
|
}
|
|
}
|
|
|
|
|
|
fn render<'r, 'data: 'r>(&self, target: &mut RenderTarget<'r, 'data>) {
|
|
fn render<'r, 'data: 'r>(&self, target: &mut RenderTarget<'r, 'data>) {
|
|
target
|
|
target
|
|
- .with_node(&self.layout_root)
|
|
|
|
|
|
+ .with_node(&self.root_layout)
|
|
.fill(match self.state {
|
|
.fill(match self.state {
|
|
ButtonState::Idle => ColourChoice::Background,
|
|
ButtonState::Idle => ColourChoice::Background,
|
|
ButtonState::Hovered => ColourChoice::Panel,
|
|
ButtonState::Hovered => ColourChoice::Panel,
|
|
ButtonState::Clicked => ColourChoice::Active,
|
|
ButtonState::Clicked => ColourChoice::Active,
|
|
})
|
|
})
|
|
.border()
|
|
.border()
|
|
|
|
+ .finish();
|
|
|
|
+ target.with_node(&self.label_layout)
|
|
.simple_text(&self.text_cache, ColourChoice::Foreground)
|
|
.simple_text(&self.text_cache, ColourChoice::Foreground)
|
|
.finish();
|
|
.finish();
|
|
}
|
|
}
|