| 1 | use eframe::egui::{self, Color32, FontId, Stroke, TextStyle}; |
| 2 | |
| 3 | pub const BACKGROUND: Color32 = Color32::from_rgb(247, 248, 250); |
| 4 | pub const PANEL: Color32 = Color32::WHITE; |
| 5 | pub const STRIPE: Color32 = Color32::from_rgb(243, 245, 248); |
| 6 | pub const BORDER: Color32 = Color32::from_rgb(217, 222, 229); |
| 7 | pub const TEXT: Color32 = Color32::from_rgb(31, 41, 51); |
| 8 | pub const MUTED: Color32 = Color32::from_rgb(91, 102, 115); |
| 9 | pub const BLUE: Color32 = Color32::from_rgb(37, 99, 235); |
| 10 | pub const BLUE_HOVER: Color32 = Color32::from_rgb(29, 78, 216); |
| 11 | pub const BLUE_ACTIVE: Color32 = Color32::from_rgb(30, 64, 175); |
| 12 | pub const SELECTED: Color32 = Color32::from_rgb(232, 240, 254); |
| 13 | pub const DROP: Color32 = Color32::from_rgb(238, 244, 255); |
| 14 | pub const DASH: Color32 = Color32::from_rgb(170, 180, 192); |
| 15 | pub const SUCCESS: Color32 = Color32::from_rgb(21, 128, 61); |
| 16 | pub const SUCCESS_BG: Color32 = Color32::from_rgb(232, 245, 236); |
| 17 | pub const WARNING: Color32 = Color32::from_rgb(180, 83, 9); |
| 18 | pub const WARNING_BG: Color32 = Color32::from_rgb(255, 247, 232); |
| 19 | pub const ERROR: Color32 = Color32::from_rgb(185, 28, 28); |
| 20 | pub const ERROR_BG: Color32 = Color32::from_rgb(253, 237, 237); |
| 21 | pub const FOLDER: Color32 = Color32::from_rgb(224, 165, 38); |
| 22 | pub const FILE: Color32 = Color32::from_rgb(123, 135, 148); |
| 23 | pub const SPACE: f32 = 8.0; |
| 24 | pub const PAD: i8 = 16; |
| 25 | pub const ROW: f32 = 32.0; |
| 26 | pub const BUTTON: f32 = 40.0; |
| 27 | pub const BODY: f32 = 15.0; |
| 28 | pub const TITLE: f32 = 22.0; |
| 29 | |
| 30 | pub fn apply(ctx: &egui::Context) { |
| 31 | ctx.set_theme(egui::Theme::Light); |
| 32 | ctx.set_visuals(egui::Visuals::light()); |
| 33 | ctx.style_mut_of(egui::Theme::Light, |style| { |
| 34 | style |
| 35 | .text_styles |
| 36 | .insert(TextStyle::Body, FontId::proportional(BODY)); |
| 37 | style |
| 38 | .text_styles |
| 39 | .insert(TextStyle::Button, FontId::proportional(BODY)); |
| 40 | style |
| 41 | .text_styles |
| 42 | .insert(TextStyle::Small, FontId::proportional(BODY)); |
| 43 | style |
| 44 | .text_styles |
| 45 | .insert(TextStyle::Heading, FontId::proportional(TITLE)); |
| 46 | style.visuals.override_text_color = Some(TEXT); |
| 47 | style.visuals.panel_fill = BACKGROUND; |
| 48 | style.visuals.window_fill = PANEL; |
| 49 | style.visuals.extreme_bg_color = PANEL; |
| 50 | style.visuals.faint_bg_color = STRIPE; |
| 51 | style.visuals.selection.bg_fill = SELECTED; |
| 52 | style.visuals.selection.stroke = Stroke::new(2.0, BLUE); |
| 53 | style.visuals.widgets.inactive.bg_fill = PANEL; |
| 54 | style.visuals.widgets.inactive.weak_bg_fill = PANEL; |
| 55 | style.visuals.widgets.inactive.bg_stroke = Stroke::new(1.0, BORDER); |
| 56 | style.visuals.widgets.hovered.bg_fill = DROP; |
| 57 | style.visuals.widgets.hovered.weak_bg_fill = DROP; |
| 58 | style.visuals.widgets.hovered.bg_stroke = Stroke::new(1.0, BLUE); |
| 59 | style.visuals.widgets.active.bg_stroke = Stroke::new(2.0, BLUE); |
| 60 | for widget in [ |
| 61 | &mut style.visuals.widgets.inactive, |
| 62 | &mut style.visuals.widgets.hovered, |
| 63 | &mut style.visuals.widgets.active, |
| 64 | &mut style.visuals.widgets.noninteractive, |
| 65 | ] { |
| 66 | widget.corner_radius = egui::CornerRadius::same(6); |
| 67 | widget.fg_stroke = Stroke::new(1.5, TEXT); |
| 68 | } |
| 69 | style.visuals.window_corner_radius = egui::CornerRadius::same(8); |
| 70 | style.spacing.item_spacing = egui::vec2(SPACE, SPACE); |
| 71 | style.spacing.button_padding = egui::vec2(16.0, SPACE); |
| 72 | style.spacing.interact_size.y = ROW; |
| 73 | style.spacing.window_margin = egui::Margin::same(PAD); |
| 74 | style.visuals.disabled_alpha = 0.4; |
| 75 | }); |
| 76 | } |
| 77 | |
| 78 | pub fn focus(response: &egui::Response) { |
| 79 | if response.has_focus() { |
| 80 | response.ctx.layer_painter(response.layer_id).rect_stroke( |
| 81 | response.rect.expand(2.0), |
| 82 | 6, |
| 83 | Stroke::new(2.0, BLUE), |
| 84 | egui::StrokeKind::Outside, |
| 85 | ); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | pub fn action(ui: &mut egui::Ui, text: &str, primary: bool, enabled: bool) -> egui::Response { |
| 90 | let response = ui |
| 91 | .scope(|ui| { |
| 92 | if primary { |
| 93 | let style = ui.style_mut(); |
| 94 | style.visuals.widgets.inactive.bg_fill = BLUE; |
| 95 | style.visuals.widgets.inactive.weak_bg_fill = BLUE; |
| 96 | style.visuals.widgets.hovered.bg_fill = BLUE_HOVER; |
| 97 | style.visuals.widgets.hovered.weak_bg_fill = BLUE_HOVER; |
| 98 | style.visuals.widgets.active.bg_fill = BLUE_ACTIVE; |
| 99 | style.visuals.widgets.active.weak_bg_fill = BLUE_ACTIVE; |
| 100 | } |
| 101 | ui.add_enabled( |
| 102 | enabled, |
| 103 | egui::Button::new(egui::RichText::new(text).strong().color(if primary { |
| 104 | PANEL |
| 105 | } else if text == "취소" { |
| 106 | ERROR |
| 107 | } else { |
| 108 | TEXT |
| 109 | })) |
| 110 | .min_size(egui::vec2( |
| 111 | if primary { 112.0 } else { 0.0 }, |
| 112 | if primary { BUTTON } else { 36.0 }, |
| 113 | )) |
| 114 | .stroke(Stroke::new(1.0, if primary { BLUE } else { BORDER })), |
| 115 | ) |
| 116 | }) |
| 117 | .inner; |
| 118 | focus(&response); |
| 119 | response |
| 120 | } |
| 121 | |
| 122 | pub fn panel(fill: Color32) -> egui::Frame { |
| 123 | egui::Frame::new() |
| 124 | .fill(fill) |
| 125 | .inner_margin(egui::Margin::same(PAD)) |
| 126 | .corner_radius(8) |
| 127 | } |
| 128 | |
| 129 | #[derive(Clone, Copy)] |
| 130 | pub enum Icon { |
| 131 | File, |
| 132 | Folder, |
| 133 | Archive, |
| 134 | Lock, |
| 135 | Success, |
| 136 | Warning, |
| 137 | Error, |
| 138 | } |
| 139 | |
| 140 | pub fn icon(ui: &mut egui::Ui, kind: Icon, size: f32) { |
| 141 | let (rect, _) = ui.allocate_exact_size(egui::vec2(size, size), egui::Sense::hover()); |
| 142 | let p = ui.painter(); |
| 143 | let color = match kind { |
| 144 | Icon::Folder => FOLDER, |
| 145 | Icon::Archive => BLUE, |
| 146 | Icon::Lock | Icon::Warning => WARNING, |
| 147 | Icon::Success => SUCCESS, |
| 148 | Icon::Error => ERROR, |
| 149 | Icon::File => FILE, |
| 150 | }; |
| 151 | let s = Stroke::new(if size > 32.0 { 2.5 } else { 1.5 }, color); |
| 152 | let point = |x, y| rect.min + egui::vec2(x * size, y * size); |
| 153 | let box_at = |x, y, w, h| egui::Rect::from_min_max(point(x, y), point(x + w, y + h)); |
| 154 | match kind { |
| 155 | Icon::Folder => { |
| 156 | p.rect_filled(box_at(0.08, 0.22, 0.42, 0.22), 2, color); |
| 157 | p.rect_filled(box_at(0.08, 0.34, 0.84, 0.50), 2, color); |
| 158 | } |
| 159 | Icon::File | Icon::Archive => { |
| 160 | p.rect_stroke( |
| 161 | box_at(0.19, 0.08, 0.62, 0.84), |
| 162 | 2, |
| 163 | s, |
| 164 | egui::StrokeKind::Inside, |
| 165 | ); |
| 166 | p.line_segment([point(0.60, 0.08), point(0.60, 0.30)], s); |
| 167 | p.line_segment([point(0.60, 0.30), point(0.81, 0.30)], s); |
| 168 | if matches!(kind, Icon::Archive) { |
| 169 | for y in [0.42, 0.57, 0.72] { |
| 170 | p.line_segment([point(0.39, y), point(0.59, y)], s); |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | Icon::Lock => { |
| 175 | p.circle_stroke(point(0.50, 0.35), size * 0.23, s); |
| 176 | p.rect_filled(box_at(0.18, 0.39, 0.64, 0.47), 2, color); |
| 177 | } |
| 178 | Icon::Success | Icon::Error => { |
| 179 | p.circle_stroke(rect.center(), size * 0.40, s); |
| 180 | if matches!(kind, Icon::Success) { |
| 181 | p.line_segment([point(0.28, 0.50), point(0.44, 0.66)], s); |
| 182 | p.line_segment([point(0.44, 0.66), point(0.74, 0.33)], s); |
| 183 | } else { |
| 184 | p.line_segment([point(0.34, 0.34), point(0.66, 0.66)], s); |
| 185 | p.line_segment([point(0.66, 0.34), point(0.34, 0.66)], s); |
| 186 | } |
| 187 | } |
| 188 | Icon::Warning => { |
| 189 | p.add(egui::Shape::closed_line( |
| 190 | vec![point(0.50, 0.08), point(0.94, 0.86), point(0.06, 0.86)], |
| 191 | s, |
| 192 | )); |
| 193 | p.line_segment([point(0.50, 0.32), point(0.50, 0.57)], s); |
| 194 | p.circle_filled(point(0.50, 0.73), size * 0.035, color); |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | pub fn column_label( |
| 200 | ui: &mut egui::Ui, |
| 201 | text: impl Into<egui::WidgetText>, |
| 202 | width: f32, |
| 203 | align: egui::Align, |
| 204 | ) -> egui::Response { |
| 205 | let layout = if align == egui::Align::Max { |
| 206 | egui::Layout::right_to_left(egui::Align::Center) |
| 207 | } else { |
| 208 | egui::Layout::left_to_right(egui::Align::Center) |
| 209 | }; |
| 210 | ui.allocate_ui_with_layout(egui::vec2(width, ROW), layout, |ui| { |
| 211 | ui.set_min_width(width); |
| 212 | ui.set_max_width(width); |
| 213 | ui.add(egui::Label::new(text).truncate()) |
| 214 | }) |
| 215 | .inner |
| 216 | } |
| 217 | |
| 218 | pub fn action_pair( |
| 219 | ui: &mut egui::Ui, |
| 220 | secondary: &str, |
| 221 | primary: Option<(&str, bool)>, |
| 222 | ) -> (bool, bool) { |
| 223 | let text_width = |text: &str| { |
| 224 | ui.painter() |
| 225 | .layout_no_wrap(text.into(), FontId::proportional(BODY), TEXT) |
| 226 | .size() |
| 227 | .x |
| 228 | }; |
| 229 | let width = text_width(secondary) |
| 230 | + 32.0 |
| 231 | + primary.map_or(0.0, |(label, _)| { |
| 232 | SPACE + (text_width(label) + 32.0).max(112.0) |
| 233 | }); |
| 234 | ui.horizontal(|ui| { |
| 235 | ui.with_layout(egui::Layout::left_to_right(egui::Align::Center), |ui| { |
| 236 | ui.add_space((ui.available_width() - width).max(0.0)); |
| 237 | let secondary = action(ui, secondary, false, true).clicked(); |
| 238 | let primary = primary.is_some_and(|(label, enabled)| { |
| 239 | action(ui, label, true, enabled) |
| 240 | .on_disabled_hover_text(match label { |
| 241 | "만들기" => "묶을 파일이나 폴더를 추가하세요", |
| 242 | "다른 폴더에 풀기" => "기존 풀 위치 폴더를 먼저 확인하세요", |
| 243 | _ => "입력한 내용을 확인하세요", |
| 244 | }) |
| 245 | .clicked() |
| 246 | }); |
| 247 | (secondary, primary) |
| 248 | }) |
| 249 | .inner |
| 250 | }) |
| 251 | .inner |
| 252 | } |
| 253 | |
| 254 | pub fn indeterminate(ui: &mut egui::Ui) { |
| 255 | let (rect, response) = ui.allocate_exact_size( |
| 256 | egui::vec2(ui.available_width(), SPACE * 2.0), |
| 257 | egui::Sense::hover(), |
| 258 | ); |
| 259 | ui.painter().rect_filled(rect, 8, BORDER); |
| 260 | let phase = if ui.style().animation_time > 0.0 { |
| 261 | ui.input(|i| (i.time * 0.6).fract()) as f32 |
| 262 | } else { |
| 263 | 0.5 |
| 264 | }; |
| 265 | let width = (SPACE * 8.0).min(rect.width()); |
| 266 | let pulse = egui::Rect::from_min_size( |
| 267 | rect.min + egui::vec2((rect.width() - width) * phase, 0.0), |
| 268 | egui::vec2(width, rect.height()), |
| 269 | ); |
| 270 | ui.painter().rect_filled(pulse, 8, BLUE); |
| 271 | response.widget_info(|| { |
| 272 | egui::WidgetInfo::labeled( |
| 273 | egui::Role::ProgressIndicator, |
| 274 | ui.is_enabled(), |
| 275 | "전체 크기를 알 수 없는 작업 진행 중", |
| 276 | ) |
| 277 | }); |
| 278 | } |