pushrod/layouts/grid_layout.rs
1// Pushrod Rendering Library
2// Grid Layout Manager
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use crate::render::layout::{Layout, LayoutPosition};
17use crate::render::widget_cache::WidgetContainer;
18use crate::render::widget_config::PaddingConstraint;
19
20/// This is the `GridLayout` storage structure for the `GridLayout` implementation.
21pub struct GridLayout {
22 widget_ids: Vec<i32>,
23 // widget_positions: Vec<LayoutPosition>,
24 // origin: Points,
25 // size: Size,
26 padding: PaddingConstraint,
27 // layout: Vec<i32>,
28 invalidated: bool,
29}
30
31/// Creates a new `GridLayout` manager.
32impl GridLayout {
33 pub fn new(
34 _x: i32,
35 _y: i32,
36 _w: u32,
37 _h: u32,
38 _layout: Vec<i32>,
39 padding: PaddingConstraint,
40 ) -> Self {
41 Self {
42 widget_ids: Vec::new(),
43 // widget_positions: Vec::new(),
44 // origin: vec![x, y],
45 // size: vec![w, h],
46 padding,
47 // layout,
48 invalidated: false,
49 }
50 }
51}
52
53/// This is the `Layout` implementation for the `GridLayout` manager. This `Layout` manager will
54/// not reposition any objects within the bounds of the `Layout` until at least 2 objects have been
55/// added to the bounds of the `Layout`.
56impl Layout for GridLayout {
57 /// Adds a widget to the `HorizontalLayout` managed stack.
58 fn insert_widget(&mut self, _widget_id: i32, _widget_position: LayoutPosition) {
59 // self.widget_ids.push(widget_id);
60 // self.widget_positions.push(widget_position);
61 // self.invalidated = true;
62 }
63
64 /// Appends a widget to the `HorizontalLayout` managed stack.
65 fn append_widget(&mut self, _widget_id: i32) {
66 // let positions = self.widget_positions.len();
67 // let widget_position = if self.widget_positions.is_empty() {
68 // LayoutPosition::new(0, 0)
69 // } else {
70 // LayoutPosition::new(0, self.widget_positions[positions - 1].y + 1)
71 // };
72 //
73 // self.insert_widget(widget_id, widget_position);
74 }
75
76 fn set_padding(&mut self, padding: PaddingConstraint) {
77 self.padding = padding;
78 self.invalidated = true;
79 }
80
81 fn get_padding(&self) -> PaddingConstraint {
82 self.padding
83 }
84
85 /// Adjusts the layout of the `Widget`s managed by this `Layout` manager. Currently only obeys
86 /// the spacing in the object. The rest of the padding is not (yet) honored.
87 fn do_layout(&mut self, _widgets: &[WidgetContainer]) {
88 if self.widget_ids.len() <= 1 {
89 return;
90 }
91
92 // let offset_x: i32 = self.origin[0];
93 // let offset_y: i32 = self.origin[1] + self.padding.top;
94 // let num_widgets = self.widget_ids.len() as u32;
95 // let widget_width = self.size[SIZE_WIDTH] / num_widgets as u32;
96 // let widget_height = self.size[SIZE_HEIGHT] / num_widgets as u32;
97 // let subtractor_right = ((self.padding.spacing as f64 / 2.0).ceil()) as u32;
98 // let subtractor_left = ((self.padding.spacing as f64 / 2.0).floor()) as u32;
99 // let subtractor_bottom = ((self.padding.spacing as f64 / 2.0).ceil()) as u32;
100 // let subtractor_top = ((self.padding.spacing as f64 / 2.0).floor()) as u32;
101 //
102 // for i in 0..num_widgets {
103 // let set_x: i32;
104 // let set_y: i32;
105 // let mut set_height: u32 = widget_height;
106 // let mut set_width: u32 = widget_width;
107 // let widget_id = self.widget_ids[i as usize];
108 //
109 // if i == 0 {
110 // set_x = (i * set_width) as i32 + self.padding.left;
111 // set_y = (i * set_height) as i32 + self.padding.top;
112 // set_height = widget_height - subtractor_bottom - self.padding.top as u32;
113 // set_width = widget_width - subtractor_right - self.padding.left as u32;
114 // } else if i == num_widgets - 1 {
115 // set_x = (i * set_width) as i32 + subtractor_left as i32;
116 // set_y = (i * set_height) as i32 + subtractor_top as i32;
117 // set_height = widget_height - subtractor_top - self.padding.bottom as u32;
118 // set_width = widget_width - subtractor_left - self.padding.right as u32;
119 // } else {
120 // set_x = (i * set_width) as i32 + subtractor_left as i32;
121 // set_y = (i * set_height) as i32 + subtractor_top as i32;
122 // set_height = widget_height - subtractor_top - subtractor_bottom;
123 // set_width = widget_width - subtractor_left - subtractor_right;
124 // }
125 //
126 // _widgets[widget_id as usize]
127 // .widget
128 // .borrow_mut()
129 // .get_config()
130 // .set_point(CONFIG_ORIGIN, offset_x + set_x, offset_y + set_y);
131 //
132 // _widgets[widget_id as usize]
133 // .widget
134 // .borrow_mut()
135 // .get_config()
136 // .set_size(
137 // CONFIG_SIZE,
138 // self.size[SIZE_WIDTH] - self.padding.right as u32 - self.padding.left as u32,
139 // self.size[SIZE_HEIGHT] - self.padding.top as u32 - self.padding.bottom as u32,
140 // );
141 //
142 // _widgets[widget_id as usize]
143 // .widget
144 // .borrow_mut()
145 // .get_config()
146 // .set_invalidated(true);
147 // }
148
149 self.invalidated = false;
150 }
151
152 fn needs_layout(&self) -> bool {
153 self.invalidated
154 }
155}