123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- use kahlo::{math::PixelSideOffsets, prelude::*};
- use crate::{
- component::Component,
- layout::{LayoutNode, LayoutNodeAccess, LayoutNodeContainer, SizePolicy},
- theme::Theme,
- ui::UIHandle,
- window::RenderFormat,
- };
- use super::Widget;
- pub struct Frame<C: Component> {
- layout: LayoutNode,
- child: Option<Box<dyn Widget<C>>>,
- _ghost: std::marker::PhantomData<C>,
- }
- impl<C: Component> Frame<C> {
- pub fn new(uih: &UIHandle) -> Self {
- let mut nnode = uih.new_layout_node();
- nnode
- .set_height_policy(SizePolicy::expanding(1))
- .set_width_policy(SizePolicy::expanding(1));
- Self {
- layout: nnode,
- child: None,
- _ghost: Default::default(),
- }
- }
- pub fn set_child(&mut self, child: Box<dyn Widget<C>>) {
- self.child = Some(child);
- self.layout.relayout();
- }
- }
- impl<C: Component> LayoutNodeContainer for Frame<C> {
- fn layout_node(&self) -> &LayoutNode {
- &self.layout
- }
- fn layout_child(&self, ndx: usize) -> Option<LayoutNodeAccess> {
- if ndx == 0 {
- Some(self.child.as_ref()?.layout_node())
- } else {
- None
- }
- }
- fn layout_child_count(&self) -> usize {
- if self.child.is_some() {
- 1
- } else {
- 0
- }
- }
- }
- 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 {
- LayoutNodeAccess::new(self)
- }
- fn layout_node_mut(&mut self) -> &mut LayoutNode {
- &mut self.layout
- }
- fn render(&self, theme: &Theme, target: &mut kahlo::BitmapMut<RenderFormat>) {
- let area = self.layout.render_area().unwrap();
- target.fill_region(area, theme.border);
- target.fill_region(
- area.inner_box(PixelSideOffsets::new_all_same(theme.border_width as i32)),
- theme.panel,
- );
- }
- }
|