| 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 |
#![forbid(unsafe_code)] |
| 38 |
|
| 39 |
use egui::{Color32, CornerRadius, Margin, Painter, Rect, Shape, Stroke, Ui}; |
| 40 |
use makeover_layout::{Bevel, Depth, Edge, Fill}; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 49 |
pub struct Palette { |
| 50 |
|
| 51 |
pub page: Color32, |
| 52 |
|
| 53 |
pub raised: Color32, |
| 54 |
|
| 55 |
pub overlay: Color32, |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
pub well: Color32, |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
pub sunken: Color32, |
| 74 |
|
| 75 |
pub bevel_light: Color32, |
| 76 |
|
| 77 |
pub bevel_dark: Color32, |
| 78 |
} |
| 79 |
|
| 80 |
impl Palette { |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
#[must_use] |
| 94 |
pub const fn fill(&self, fill: Fill) -> Option<Color32> { |
| 95 |
match fill { |
| 96 |
Fill::Page => Some(self.page), |
| 97 |
Fill::Raised => Some(self.raised), |
| 98 |
Fill::Overlay => Some(self.overlay), |
| 99 |
Fill::Well => Some(self.well), |
| 100 |
Fill::Sunken => Some(self.sunken), |
| 101 |
_ => None, |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
#[must_use] |
| 107 |
pub const fn edge(&self, edge: Edge) -> Color32 { |
| 108 |
match edge { |
| 109 |
Edge::Light => self.bevel_light, |
| 110 |
Edge::Dark => self.bevel_dark, |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
#[derive(Debug, Clone, Copy, PartialEq)] |
| 121 |
pub struct FrameStyle { |
| 122 |
|
| 123 |
pub radius: CornerRadius, |
| 124 |
|
| 125 |
pub margin: Margin, |
| 126 |
|
| 127 |
pub stroke: f32, |
| 128 |
} |
| 129 |
|
| 130 |
impl Default for FrameStyle { |
| 131 |
|
| 132 |
fn default() -> Self { |
| 133 |
Self { |
| 134 |
radius: CornerRadius::ZERO, |
| 135 |
margin: Margin::ZERO, |
| 136 |
stroke: 1.0, |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
pub fn paint_bevel(painter: &Painter, rect: Rect, bevel: Bevel, palette: &Palette, stroke: f32) { |
| 160 |
let (top_left, bottom_right) = bevel.edges(); |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
let r = rect.shrink(stroke / 2.0); |
| 166 |
|
| 167 |
painter.add(Shape::line( |
| 168 |
vec![r.left_bottom(), r.left_top(), r.right_top()], |
| 169 |
Stroke::new(stroke, palette.edge(top_left)), |
| 170 |
)); |
| 171 |
painter.add(Shape::line( |
| 172 |
vec![r.right_top(), r.right_bottom(), r.left_bottom()], |
| 173 |
Stroke::new(stroke, palette.edge(bottom_right)), |
| 174 |
)); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
pub fn frame<R>( |
| 184 |
ui: &mut Ui, |
| 185 |
depth: Depth, |
| 186 |
palette: &Palette, |
| 187 |
style: FrameStyle, |
| 188 |
add_contents: impl FnOnce(&mut Ui) -> R, |
| 189 |
) -> R { |
| 190 |
let mut f = egui::Frame::new() |
| 191 |
.corner_radius(style.radius) |
| 192 |
.inner_margin(style.margin); |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
if let Some(fill) = depth.fill().and_then(|f| palette.fill(f)) { |
| 199 |
f = f.fill(fill); |
| 200 |
} |
| 201 |
let framed = f.show(ui, add_contents); |
| 202 |
if let Some(bevel) = depth.bevel() { |
| 203 |
paint_bevel( |
| 204 |
ui.painter(), |
| 205 |
framed.response.rect, |
| 206 |
bevel, |
| 207 |
palette, |
| 208 |
style.stroke, |
| 209 |
); |
| 210 |
} |
| 211 |
framed.inner |
| 212 |
} |
| 213 |
|
| 214 |
#[cfg(test)] |
| 215 |
mod tests { |
| 216 |
use super::*; |
| 217 |
|
| 218 |
fn palette(well: Color32) -> Palette { |
| 219 |
Palette { |
| 220 |
page: Color32::from_rgb(1, 1, 1), |
| 221 |
raised: Color32::from_rgb(2, 2, 2), |
| 222 |
overlay: Color32::from_rgb(3, 3, 3), |
| 223 |
well, |
| 224 |
sunken: Color32::from_rgb(4, 4, 4), |
| 225 |
bevel_light: Color32::WHITE, |
| 226 |
bevel_dark: Color32::BLACK, |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
#[test] |
| 231 |
fn a_well_resolves_to_its_own_token() { |
| 232 |
|
| 233 |
|
| 234 |
let w = Color32::from_rgb(9, 9, 9); |
| 235 |
let p = palette(w); |
| 236 |
assert_eq!(p.fill(Fill::Well), Some(w)); |
| 237 |
assert_ne!(p.fill(Fill::Well), Some(p.page)); |
| 238 |
} |
| 239 |
|
| 240 |
#[test] |
| 241 |
fn every_intent_is_a_plain_lookup() { |
| 242 |
let p = palette(Color32::from_rgb(9, 9, 9)); |
| 243 |
assert_eq!(p.fill(Fill::Page), Some(p.page)); |
| 244 |
assert_eq!(p.fill(Fill::Raised), Some(p.raised)); |
| 245 |
assert_eq!(p.fill(Fill::Overlay), Some(p.overlay)); |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
#[test] |
| 252 |
fn sunken_is_neither_the_well_nor_the_page() { |
| 253 |
let p = palette(Color32::from_rgb(9, 9, 9)); |
| 254 |
assert_eq!(p.fill(Fill::Sunken), Some(p.sunken)); |
| 255 |
assert_ne!(p.fill(Fill::Sunken), p.fill(Fill::Well)); |
| 256 |
assert_ne!(p.fill(Fill::Sunken), p.fill(Fill::Page)); |
| 257 |
} |
| 258 |
|
| 259 |
#[test] |
| 260 |
fn a_raised_region_never_resolves_to_the_well_fill() { |
| 261 |
|
| 262 |
let p = palette(Color32::from_rgb(9, 9, 9)); |
| 263 |
let raised = Depth::Raised.fill().and_then(|f| p.fill(f)); |
| 264 |
let well = Depth::Well.fill().and_then(|f| p.fill(f)); |
| 265 |
assert_eq!(raised, Some(p.raised)); |
| 266 |
assert_ne!(raised, well); |
| 267 |
} |
| 268 |
|
| 269 |
#[test] |
| 270 |
fn the_lit_edge_swaps_when_a_card_is_pressed() { |
| 271 |
let p = palette(Color32::from_rgb(9, 9, 9)); |
| 272 |
let (tl, _) = Depth::Raised.bevel().unwrap().edges(); |
| 273 |
let (ptl, _) = Depth::Raised.pressed().bevel().unwrap().edges(); |
| 274 |
assert_eq!(p.edge(tl), p.bevel_light); |
| 275 |
assert_eq!(p.edge(ptl), p.bevel_dark); |
| 276 |
} |
| 277 |
|
| 278 |
#[test] |
| 279 |
fn flat_asks_for_neither_fill_nor_edge() { |
| 280 |
assert!(Depth::Flat.fill().is_none()); |
| 281 |
assert!(Depth::Flat.bevel().is_none()); |
| 282 |
} |
| 283 |
|
| 284 |
#[test] |
| 285 |
fn the_default_frame_is_square_and_one_point() { |
| 286 |
let d = FrameStyle::default(); |
| 287 |
assert_eq!(d.radius, CornerRadius::ZERO); |
| 288 |
assert_eq!(d.margin, Margin::ZERO); |
| 289 |
assert!((d.stroke - 1.0).abs() < f32::EPSILON); |
| 290 |
} |
| 291 |
} |
| 292 |
|