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