pushrod/render/
layout.rs

1// Pushrod Rendering Library
2// Extensible Layout Library
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::widget_cache::WidgetContainer;
17use crate::render::widget_config::PaddingConstraint;
18
19/// This is a structure that describes the position of a `Widget` within its `Layout`.  `X` and
20/// `Y` coordinates are not given as physical positions on the screen, rather, their position in the
21/// `Layout` matrix.
22pub struct LayoutPosition {
23    pub x: i32,
24    pub y: i32,
25}
26
27/// Implementation of the `LayoutPosition` that generates a new `LayoutPosition` object.
28impl LayoutPosition {
29    pub fn new(x: i32, y: i32) -> Self {
30        Self { x, y }
31    }
32}
33
34/// This is a `Layout` trait that is used by the `Engine` service, which stores a list of `Widget`s,
35/// their positions (based on matrix coordinates), and an entry point to trigger the layout compute
36/// action.
37pub trait Layout {
38    /// Adds a `Widget` by ID to the `Layout` manager, given its `LayoutPosition`, as a position
39    /// marker in the manager.
40    fn insert_widget(&mut self, _widget_id: i32, _widget_position: LayoutPosition);
41
42    /// Adds a `Widget` by ID to the `Layout` manager, automatically adding it to the next available
43    /// `LayoutPosition`.  Use this as a way to add a widget if you don't need to specify a
44    /// `LayoutPosition`.
45    fn append_widget(&mut self, _widget_id: i32);
46
47    /// Changes the `PaddingConstraint` for this `Layout`.
48    fn set_padding(&mut self, padding: PaddingConstraint);
49
50    /// Retrieves the current `PaddingConstraint`.
51    fn get_padding(&self) -> PaddingConstraint;
52
53    /// Performs a layout, applying the `WidgetContainer` list at the time, so that referenced
54    /// `Widget`s can be adjusted as necessary.
55    fn do_layout(&mut self, _widgets: &[WidgetContainer]);
56
57    /// Indicates whether or not the `Layout` needs to have `do_layout` re-run.  This is generally
58    /// needed when the `LayoutPosition` changes, or when `PaddingConstraint`s change.
59    fn needs_layout(&self) -> bool;
60}