1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use std::any::Any;
use std::f32;
use kas::draw::{self, DrawText, FontId, TextClass};
use kas::geom::Size;
use kas::layout::{AxisInfo, SizeRules, StretchPolicy};
use kas::Direction::{Horizontal, Vertical};
#[derive(Clone, Debug)]
pub struct DimensionsParams {
pub margin: f32,
pub frame_size: f32,
pub button_frame: f32,
pub scrollbar_size: f32,
}
#[derive(Clone, Debug)]
pub struct Dimensions {
pub font_id: FontId,
pub font_scale: f32,
pub line_height: u32,
pub min_line_length: u32,
pub max_line_length: u32,
pub margin: u32,
pub frame: u32,
pub button_frame: u32,
pub checkbox: u32,
pub scrollbar: u32,
}
impl Dimensions {
pub fn new(params: DimensionsParams, font_id: FontId, font_size: f32, dpi_factor: f32) -> Self {
let font_scale = font_size * dpi_factor;
let line_height = font_scale.round() as u32;
let margin = (params.margin * dpi_factor).round() as u32;
let frame = (params.frame_size * dpi_factor).round() as u32;
Dimensions {
font_id,
font_scale,
line_height,
min_line_length: line_height * 10,
max_line_length: line_height * 40,
margin,
frame,
button_frame: (params.button_frame * dpi_factor).round() as u32,
checkbox: (font_scale * 0.7).round() as u32 + 2 * (margin + frame),
scrollbar: (params.scrollbar_size * dpi_factor).round() as u32,
}
}
}
pub struct DimensionsWindow {
pub dims: Dimensions,
}
impl DimensionsWindow {
pub fn new(dims: DimensionsParams, font_id: FontId, font_size: f32, dpi_factor: f32) -> Self {
DimensionsWindow {
dims: Dimensions::new(dims, font_id, font_size, dpi_factor),
}
}
}
impl<Draw: DrawText + 'static> crate::Window<Draw> for DimensionsWindow {
#[cfg(not(feature = "gat"))]
type SizeHandle = SizeHandle<'static, Draw>;
#[cfg(feature = "gat")]
type SizeHandle<'a> = SizeHandle<'a, Draw>;
#[cfg(not(feature = "gat"))]
unsafe fn size_handle<'a>(&'a mut self, draw: &'a mut Draw) -> Self::SizeHandle {
let h: SizeHandle<'a, Draw> = SizeHandle::new(draw, &self.dims);
std::mem::transmute(h)
}
#[cfg(feature = "gat")]
fn size_handle<'a>(&'a mut self, draw: &'a mut Draw) -> Self::SizeHandle<'a> {
SizeHandle::new(draw, &self.dims)
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}
pub struct SizeHandle<'a, Draw> {
draw: &'a mut Draw,
dims: &'a Dimensions,
}
impl<'a, Draw> SizeHandle<'a, Draw> {
pub fn new(draw: &'a mut Draw, dims: &'a Dimensions) -> Self {
SizeHandle { draw, dims }
}
}
impl<'a, Draw: DrawText> draw::SizeHandle for SizeHandle<'a, Draw> {
fn outer_frame(&self) -> (Size, Size) {
let f = self.dims.frame as u32;
(Size::uniform(f), Size::uniform(f))
}
fn inner_margin(&self) -> Size {
Size::uniform(self.dims.margin as u32)
}
fn outer_margin(&self) -> Size {
Size::uniform(self.dims.margin as u32)
}
fn line_height(&self, _: TextClass) -> u32 {
self.dims.line_height
}
fn text_bound(&mut self, text: &str, class: TextClass, axis: AxisInfo) -> SizeRules {
let font_id = self.dims.font_id;
let font_scale = self.dims.font_scale;
let line_height = self.dims.line_height;
let mut bounds = (f32::INFINITY, f32::INFINITY);
if let Some(size) = axis.size_other_if_fixed(Horizontal) {
bounds.1 = size as f32;
} else if let Some(size) = axis.size_other_if_fixed(Vertical) {
bounds.0 = size as f32;
}
let line_wrap = match class {
TextClass::Label | TextClass::EditMulti => true,
TextClass::Button | TextClass::Edit => false,
};
let bounds = self
.draw
.text_bound(text, font_id, font_scale, bounds, line_wrap);
if axis.is_horizontal() {
let bound = bounds.0 as u32;
let min = match class {
TextClass::Edit | TextClass::EditMulti => self.dims.min_line_length,
_ => bound.min(self.dims.min_line_length),
};
let ideal = bound.min(self.dims.max_line_length);
SizeRules::new(min, ideal, StretchPolicy::LowUtility)
} else {
let min = match class {
TextClass::EditMulti => line_height * 3,
_ => line_height,
};
let ideal = (bounds.1 as u32).max(line_height);
let stretch = match class {
TextClass::Button | TextClass::Edit => StretchPolicy::Fixed,
_ => StretchPolicy::Filler,
};
SizeRules::new(min, ideal, stretch)
}
}
fn button_surround(&self) -> (Size, Size) {
let s = Size::uniform(self.dims.button_frame);
(s, s)
}
fn edit_surround(&self) -> (Size, Size) {
let s = Size::uniform(self.dims.frame as u32);
(s, s)
}
fn checkbox(&self) -> Size {
Size::uniform(self.dims.checkbox)
}
#[inline]
fn radiobox(&self) -> Size {
self.checkbox()
}
fn scrollbar(&self) -> (u32, u32, u32) {
let s = self.dims.scrollbar as u32;
(s, s, 2 * s)
}
}