Browse Source

Expand theme slightly.

Kestrel 5 months ago
parent
commit
805140469f
7 changed files with 42 additions and 37 deletions
  1. 4 2
      examples/simplest.rs
  2. 1 8
      src/text.rs
  3. 7 3
      src/theme.rs
  4. 2 0
      src/widget.rs
  5. 19 20
      src/widget/frame.rs
  6. 8 3
      src/widget/label.rs
  7. 1 1
      src/window.rs

+ 4 - 2
examples/simplest.rs

@@ -8,7 +8,8 @@ impl SimpleWindow {
     fn new(uih: &mut patina::ui::UIHandle) -> Self {
         Self {
             // widget: patina::widget::Spacer::new(uih),
-            widget: patina::widget::Label::new(uih),
+            // widget: patina::widget::Label::new(uih),
+            widget: patina::widget::Frame::new(uih),
         }
     }
 }
@@ -37,7 +38,8 @@ impl WindowComponent for SimpleWindow {
     }
 
     // type RootWidget = patina::widget::Spacer<Self>;
-    type RootWidget = patina::widget::Label<Self>;
+    // type RootWidget = patina::widget::Label<Self>;
+    type RootWidget = patina::widget::Frame<Self>;
     fn root_widget(&self) -> &Self::RootWidget {
         &self.widget
     }

+ 1 - 8
src/text.rs

@@ -1,5 +1,5 @@
 use kahlo::{
-    math::{PixelBox, PixelPoint, PixelRect},
+    math::{PixelBox, PixelPoint},
     prelude::*,
 };
 
@@ -58,18 +58,11 @@ impl<'l> TextLine<'l> {
                 metrics.width,
             );
 
-            // ascent height 10
-            // descent height 4
-            // glyph height 2
-            // glyph offset: 1
-            // (should be placed in y-range [7,8])
-            // so use offset (ascent height - (glyph height + glyph offset))
             alphamap.copy_from(
                 &character_alphamap,
                 &PixelBox::from_size(character_alphamap.size()),
                 &PixelPoint::new(metrics.xmin + offset as i32, baseline as i32 - (metrics.height as i32 + metrics.ymin as i32)),
             );
-            println!("offset: {offset} ymin: {}", metrics.ymin);
         }
 
         alphamap

+ 7 - 3
src/theme.rs

@@ -4,7 +4,9 @@ use kahlo::colour::Colour;
 
 pub struct Theme {
     pub border: Colour,
+    pub border_width: usize,
     pub background: Colour,
+    pub panel: Colour,
     pub foreground: Colour,
 
     pub ui_font: fontdue::Font,
@@ -18,12 +20,14 @@ impl Theme {
 
     pub fn default_with_font(ui_font: fontdue::Font) -> Self {
         Self {
-            border: Colour::GREEN,
-            background: Colour::BLUE,
+            border: Colour::rgb(192, 168, 32),
+            border_width: 3,
+            background: Colour::rgb(32, 32, 32),
+            panel: Colour::rgb(32, 48, 32),
             foreground: Colour::WHITE,
 
             ui_font,
-            ui_font_size: 64.0,
+            ui_font_size: 16.0,
         }
     }
 }

+ 2 - 0
src/widget.rs

@@ -12,6 +12,8 @@ use crate::{
 
 mod label;
 pub use label::Label;
+mod frame;
+pub use frame::Frame;
 
 pub trait Widget<C: Component> {
     fn poll(&mut self, uih: &mut UIHandle, input_state: Option<&InputState>) -> Vec<C::Msg>;

+ 19 - 20
src/widget/frame.rs

@@ -1,36 +1,36 @@
+use kahlo::{prelude::*, math::PixelSideOffsets};
+
 use crate::{
     component::Component,
-    layout::{LayoutNode, LayoutNodeAccess, LayoutNodeContainer},
-    platform::{
-        render::{Colour, Painter},
-        PlatformPainter, PlatformSpec,
-    },
+    layout::{LayoutNode, LayoutNodeAccess, LayoutNodeContainer, SizePolicy}, ui::UIHandle, theme::Theme,
 };
 
-use super::{BuildContext, Widget};
+use super::Widget;
 
-pub struct Frame<P: PlatformSpec, C: Component> {
+pub struct Frame<C: Component> {
     layout: LayoutNode,
-    child: Option<Box<dyn Widget<P, C>>>,
+    child: Option<Box<dyn Widget<C>>>,
     _ghost: std::marker::PhantomData<C>,
 }
 
-impl<P: PlatformSpec, C: Component> Frame<P, C> {
-    pub fn new(bc: &BuildContext) -> Self {
+impl<C: Component> Frame<C> {
+    pub fn new(uih: &UIHandle) -> Self {
+        let mut nnode = uih.new_layout_node();
+        nnode.set_height_policy(SizePolicy::expands(1)).set_width_policy(SizePolicy::expands(1));
         Self {
-            layout: LayoutNode::new(bc.lcache.clone()),
+            layout: nnode,
             child: None,
             _ghost: Default::default(),
         }
     }
 
-    pub fn set_child(&mut self, child: Box<dyn Widget<P, C>>) {
+    pub fn set_child(&mut self, child: Box<dyn Widget<C>>) {
         self.child = Some(child);
         self.layout.relayout();
     }
 }
 
-impl<P: PlatformSpec, C: Component> LayoutNodeContainer for Frame<P, C> {
+impl<C: Component> LayoutNodeContainer for Frame<C> {
     fn layout_node(&self) -> &LayoutNode {
         &self.layout
     }
@@ -50,8 +50,8 @@ impl<P: PlatformSpec, C: Component> LayoutNodeContainer for Frame<P, C> {
     }
 }
 
-impl<P: PlatformSpec, C: Component> Widget<P, C> for Frame<P, C> {
-    fn poll(&mut self, _input_state: Option<&crate::input::InputState>) -> Vec<C::Msg> {
+impl<C: Component> Widget<C> for Frame<C> {
+    fn poll(&mut self, _uih: &mut UIHandle, _input_state: Option<&crate::input::InputState>) -> Vec<C::Msg> {
         vec![]
     }
     fn layout_node(&self) -> LayoutNodeAccess {
@@ -60,11 +60,10 @@ impl<P: PlatformSpec, C: Component> Widget<P, C> for Frame<P, C> {
     fn layout_node_mut(&mut self) -> &mut LayoutNode {
         &mut self.layout
     }
-    fn render(&self, painter: &mut PlatformPainter<P>, ti: &P::Text) {
+    fn render(&self, theme: &Theme, target: &mut kahlo::RgbaBitmap) {
         let rect = self.layout.cached_rect().unwrap();
-        painter.fill(rect, Colour::new_rgb(50, 42, 0));
-        if let Some(child) = self.child.as_ref() {
-            child.render(painter, ti);
-        }
+
+        target.fill_region(&rect.to_box2d(), theme.border);
+        target.fill_region(&rect.to_box2d().inner_box(PixelSideOffsets::new_all_same(theme.border_width as i32)), theme.panel);
     }
 }

+ 8 - 3
src/widget/label.rs

@@ -11,7 +11,7 @@ use super::Widget;
 
 pub struct Label<C: Component> {
     lnode: LeafLayoutNode,
-    text: String,
+    text: Option<String>,
     _ghost: std::marker::PhantomData<C>,
 }
 
@@ -26,10 +26,14 @@ impl<C: Component> Label<C> {
             });
         Self {
             lnode: LeafLayoutNode::new(node),
-            text: "example text here".to_string(),
+            text: None,
             _ghost: Default::default(),
         }
     }
+
+    pub fn set_text(&mut self, text: String) {
+        self.text = Some(text);
+    }
 }
 
 impl<C: Component> Widget<C> for Label<C> {
@@ -49,7 +53,8 @@ impl<C: Component> Widget<C> for Label<C> {
     }
 
     fn render(&self, theme: &Theme, surface: &mut kahlo::RgbaBitmap) {
-        let line = theme.make_line(self.text.as_str());
+        let Some(text) = self.text.as_ref() else { return };
+        let line = theme.make_line(text.as_str());
         let rendered = line.render_line();
 
         let render_location = self.layout_node().cached_rect().unwrap();

+ 1 - 1
src/window.rs

@@ -52,7 +52,7 @@ impl<WC: WindowComponent> WindowState<WC> {
         let layout = LinearAccess::new(&self.root_node, self.wc.root_widget().layout_node());
 
         let mut pixmap = kahlo::RgbaBitmap::new(size.width as usize, size.height as usize);
-        pixmap.fill(kahlo::colour::Colour::rgb(128, 128, 128));
+        pixmap.fill(uih.theme().background);
         layout::recalculate(LayoutNodeAccess::new(&layout));
         self.wc.root_widget().render(uih.theme(), &mut pixmap);