|
@@ -1,10 +1,14 @@
|
|
use crate::{
|
|
use crate::{
|
|
input::MouseButton,
|
|
input::MouseButton,
|
|
- layout::{HorizontalAlignment, LayoutNode, LayoutNodeAccess, LayoutTreeNode, LeafLayoutNode, SizePolicy},
|
|
|
|
|
|
+ layout::{
|
|
|
|
+ HorizontalAlignment, LayoutNode, LayoutNodeAccess, LayoutTreeNode, LeafLayoutNode,
|
|
|
|
+ SizePolicy,
|
|
|
|
+ },
|
|
prelude::*,
|
|
prelude::*,
|
|
render::{ColourChoice, RenderTarget, TextCache},
|
|
render::{ColourChoice, RenderTarget, TextCache},
|
|
ui::UIHandle,
|
|
ui::UIHandle,
|
|
};
|
|
};
|
|
|
|
+use kahlo::math::{PixelBox, PixelPoint, PixelSideOffsets, PixelSize};
|
|
use winit::keyboard::{Key, NamedKey};
|
|
use winit::keyboard::{Key, NamedKey};
|
|
|
|
|
|
pub enum CursorPosition {
|
|
pub enum CursorPosition {
|
|
@@ -14,8 +18,7 @@ pub enum CursorPosition {
|
|
}
|
|
}
|
|
|
|
|
|
pub struct TextEdit<C: Component> {
|
|
pub struct TextEdit<C: Component> {
|
|
- lnode: LayoutNode,
|
|
|
|
- cursor_node: LeafLayoutNode,
|
|
|
|
|
|
+ lnode: LeafLayoutNode,
|
|
text: TextCache,
|
|
text: TextCache,
|
|
cursor_pos: CursorPosition,
|
|
cursor_pos: CursorPosition,
|
|
onchange: Option<Box<dyn Fn(&str) -> Option<C::Msg>>>,
|
|
onchange: Option<Box<dyn Fn(&str) -> Option<C::Msg>>>,
|
|
@@ -30,22 +33,15 @@ impl<C: Component> TextEdit<C> {
|
|
.set_width_policy(SizePolicy::expanding(1))
|
|
.set_width_policy(SizePolicy::expanding(1))
|
|
.set_height_policy(SizePolicy::fixed(minheight));
|
|
.set_height_policy(SizePolicy::fixed(minheight));
|
|
|
|
|
|
- let mut cursor_node = uih.new_layout_node();
|
|
|
|
- cursor_node
|
|
|
|
- .set_width_policy(SizePolicy::fixed(2))
|
|
|
|
- .set_height_policy(SizePolicy::fixed(minheight))
|
|
|
|
- .set_halign(HorizontalAlignment::Left);
|
|
|
|
-
|
|
|
|
Self {
|
|
Self {
|
|
- lnode,
|
|
|
|
- cursor_node: LeafLayoutNode::new(cursor_node),
|
|
|
|
|
|
+ lnode: LeafLayoutNode::new(lnode),
|
|
text: TextCache::new_with_font_and_size(
|
|
text: 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,
|
|
),
|
|
),
|
|
cursor_pos: CursorPosition::End,
|
|
cursor_pos: CursorPosition::End,
|
|
onchange: None,
|
|
onchange: None,
|
|
- focused: false
|
|
|
|
|
|
+ focused: false,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -54,24 +50,8 @@ impl<C: Component> TextEdit<C> {
|
|
}
|
|
}
|
|
|
|
|
|
/*pub fn set_cursor_position(&mut self, to: CursorPosition) {
|
|
/*pub fn set_cursor_position(&mut self, to: CursorPosition) {
|
|
-
|
|
|
|
- }*/
|
|
|
|
-}
|
|
|
|
|
|
|
|
-impl<C: Component> LayoutTreeNode<()> for TextEdit<C> {
|
|
|
|
- fn child(&self, ndx: usize) -> Option<LayoutNodeAccess<'_>> {
|
|
|
|
- if ndx == 0 {
|
|
|
|
- Some(self.cursor_node.access())
|
|
|
|
- } else {
|
|
|
|
- None
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- fn child_count(&self) -> usize {
|
|
|
|
- 1
|
|
|
|
- }
|
|
|
|
- fn current_node(&self) -> &LayoutNode {
|
|
|
|
- &self.lnode
|
|
|
|
- }
|
|
|
|
|
|
+ }*/
|
|
}
|
|
}
|
|
|
|
|
|
impl<C: Component> Widget<C> for TextEdit<C> {
|
|
impl<C: Component> Widget<C> for TextEdit<C> {
|
|
@@ -113,18 +93,14 @@ impl<C: Component> Widget<C> for TextEdit<C> {
|
|
.is_click()
|
|
.is_click()
|
|
{
|
|
{
|
|
istate.focus_on(self.lnode.id());
|
|
istate.focus_on(self.lnode.id());
|
|
- self.focused = true;
|
|
|
|
println!("click offset: {:?}", istate.mouse.pos - render_area.min);
|
|
println!("click offset: {:?}", istate.mouse.pos - render_area.min);
|
|
-
|
|
|
|
- let cbar_offset = self.text.size_hint().width;
|
|
|
|
- self.cursor_node.margin_mut().left = cbar_offset;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
let was_focused = self.focused;
|
|
let was_focused = self.focused;
|
|
self.focused = istate.is_focused(self.lnode.id());
|
|
self.focused = istate.is_focused(self.lnode.id());
|
|
|
|
|
|
if self.focused != was_focused {
|
|
if self.focused != was_focused {
|
|
- self.cursor_node.render_needed();
|
|
|
|
|
|
+ println!("self.focused is now {}", self.focused);
|
|
self.lnode.render_needed();
|
|
self.lnode.render_needed();
|
|
}
|
|
}
|
|
|
|
|
|
@@ -132,7 +108,7 @@ impl<C: Component> Widget<C> for TextEdit<C> {
|
|
}
|
|
}
|
|
|
|
|
|
fn layout_node(&self) -> LayoutNodeAccess {
|
|
fn layout_node(&self) -> LayoutNodeAccess {
|
|
- LayoutNodeAccess::new(self)
|
|
|
|
|
|
+ self.lnode.access()
|
|
}
|
|
}
|
|
fn layout_node_mut(&mut self) -> &mut LayoutNode {
|
|
fn layout_node_mut(&mut self) -> &mut LayoutNode {
|
|
&mut self.lnode
|
|
&mut self.lnode
|
|
@@ -142,11 +118,18 @@ impl<C: Component> Widget<C> for TextEdit<C> {
|
|
target
|
|
target
|
|
.with_node(&self.lnode)
|
|
.with_node(&self.lnode)
|
|
.fill(ColourChoice::Background)
|
|
.fill(ColourChoice::Background)
|
|
- .simple_text(&self.text, ColourChoice::Foreground);
|
|
|
|
-
|
|
|
|
- if self.focused {
|
|
|
|
- target.with_node(&self.cursor_node)
|
|
|
|
- .fill(ColourChoice::Foreground);
|
|
|
|
- }
|
|
|
|
|
|
+ .simple_text(&self.text, ColourChoice::Foreground)
|
|
|
|
+ .seq(|target| {
|
|
|
|
+ if self.focused {
|
|
|
|
+ let cbar_offset = self.text.size_hint().width;
|
|
|
|
+ target.fill_relative_box(
|
|
|
|
+ PixelBox::from_origin_and_size(
|
|
|
|
+ PixelPoint::new(cbar_offset, 0),
|
|
|
|
+ PixelSize::new(2, self.text.size_hint().height),
|
|
|
|
+ ),
|
|
|
|
+ ColourChoice::Foreground,
|
|
|
|
+ );
|
|
|
|
+ }
|
|
|
|
+ });
|
|
}
|
|
}
|
|
}
|
|
}
|