team-ljyzipelo

공개
402줄14 KB
1use eframe::egui::{self, Color32, FontId, Stroke, TextStyle};
2
3pub const BACKGROUND: Color32 = Color32::from_rgb(247, 248, 250);
4pub const PANEL: Color32 = Color32::WHITE;
5pub const STRIPE: Color32 = Color32::from_rgb(243, 245, 248);
6pub const BORDER: Color32 = Color32::from_rgb(217, 222, 229);
7pub const TEXT: Color32 = Color32::from_rgb(31, 41, 51);
8pub const MUTED: Color32 = Color32::from_rgb(91, 102, 115);
9pub const BLUE: Color32 = Color32::from_rgb(37, 99, 235);
10pub const BLUE_HOVER: Color32 = Color32::from_rgb(29, 78, 216);
11pub const BLUE_ACTIVE: Color32 = Color32::from_rgb(30, 64, 175);
12pub const SELECTED: Color32 = Color32::from_rgb(232, 240, 254);
13pub const DROP: Color32 = Color32::from_rgb(238, 244, 255);
14pub const DASH: Color32 = Color32::from_rgb(170, 180, 192);
15pub const SUCCESS: Color32 = Color32::from_rgb(21, 128, 61);
16pub const SUCCESS_BG: Color32 = Color32::from_rgb(232, 245, 236);
17pub const WARNING: Color32 = Color32::from_rgb(180, 83, 9);
18pub const WARNING_BG: Color32 = Color32::from_rgb(255, 247, 232);
19pub const ERROR: Color32 = Color32::from_rgb(185, 28, 28);
20pub const ERROR_BG: Color32 = Color32::from_rgb(253, 237, 237);
21pub const FOLDER: Color32 = Color32::from_rgb(240, 160, 0);
22pub const FILE: Color32 = Color32::from_rgb(123, 135, 148);
23pub const SPACE: f32 = 8.0;
24pub const PAD: i8 = 16;
25pub const DROP_PAD: i8 = 24;
26pub const DROP_HEIGHT: f32 = 280.0;
27pub const DROP_RADIUS: u8 = 12;
28pub const ROW: f32 = 32.0;
29pub const BUTTON: f32 = 40.0;
30pub const BODY: f32 = 17.0;
31pub const TITLE: f32 = 22.0;
32
33pub 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
81pub 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
92pub 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
125pub 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)]
133pub enum Icon {
134 File,
135 Folder,
136 Archive,
137 Lock,
138 Success,
139 Warning,
140 Error,
141}
142
143pub 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
202pub fn bounded_text(ui: &mut egui::Ui, text: &str, width: f32) -> egui::Response {
203 let width = width.max(0.0);
204 ui.allocate_ui_with_layout(
205 egui::vec2(width, ui.text_style_height(&TextStyle::Body)),
206 ui.layout().clone(),
207 |ui| {
208 ui.set_min_width(width);
209 ui.set_max_width(width);
210 ui.add(egui::Label::new(text).truncate())
211 },
212 )
213 .inner
214 .on_hover_text(text)
215}
216
217pub fn window_title(ui: &egui::Ui, text: &str, width: f32) -> String {
218 let mut job =
219 egui::text::LayoutJob::simple_singleline(text.into(), FontId::proportional(BODY), TEXT);
220 job.wrap = egui::text::TextWrapping {
221 max_width: width.max(0.0),
222 max_rows: 1,
223 break_anywhere: true,
224 ..Default::default()
225 };
226 ui.fonts_mut(|fonts| fonts.layout_job(job))
227 .rows
228 .iter()
229 .flat_map(|row| row.glyphs.iter().map(|glyph| glyph.chr))
230 .collect()
231}
232
233pub fn column_label(
234 ui: &mut egui::Ui,
235 text: impl Into<egui::WidgetText>,
236 width: f32,
237 align: egui::Align,
238) -> egui::Response {
239 let layout = if align == egui::Align::Max {
240 egui::Layout::right_to_left(egui::Align::Center)
241 } else {
242 egui::Layout::left_to_right(egui::Align::Center)
243 };
244 ui.allocate_ui_with_layout(egui::vec2(width, ROW), layout, |ui| {
245 ui.set_min_width(width);
246 ui.set_max_width(width);
247 ui.add(egui::Label::new(text).truncate())
248 })
249 .inner
250}
251
252pub fn action_pair(
253 ui: &mut egui::Ui,
254 secondary: &str,
255 primary: Option<(&str, bool)>,
256) -> (bool, bool) {
257 let text_width = |text: &str| {
258 ui.painter()
259 .layout_no_wrap(text.into(), FontId::proportional(BODY), TEXT)
260 .size()
261 .x
262 };
263 let width = text_width(secondary)
264 + 32.0
265 + primary.map_or(0.0, |(label, _)| {
266 SPACE + (text_width(label) + 32.0).max(112.0)
267 });
268 ui.horizontal(|ui| {
269 ui.with_layout(egui::Layout::left_to_right(egui::Align::Center), |ui| {
270 ui.add_space((ui.available_width() - width).max(0.0));
271 let secondary = action(ui, secondary, false, true).clicked();
272 let primary = primary.is_some_and(|(label, enabled)| {
273 action(ui, label, true, enabled)
274 .on_disabled_hover_text(match label {
275 "만들기" => "묶을 파일이나 폴더를 추가하세요",
276 "다른 폴더에 풀기" => "기존 풀 위치 폴더를 먼저 확인하세요",
277 _ => "입력한 내용을 확인하세요",
278 })
279 .clicked()
280 });
281 (secondary, primary)
282 })
283 .inner
284 })
285 .inner
286}
287
288pub fn indeterminate(ui: &mut egui::Ui) {
289 let (rect, response) = ui.allocate_exact_size(
290 egui::vec2(ui.available_width(), SPACE * 2.0),
291 egui::Sense::hover(),
292 );
293 ui.painter().rect_filled(rect, 8, BORDER);
294 let phase = if ui.style().animation_time > 0.0 {
295 ui.input(|i| (i.time * 0.6).fract()) as f32
296 } else {
297 0.5
298 };
299 let width = (SPACE * 8.0).min(rect.width());
300 let pulse = egui::Rect::from_min_size(
301 rect.min + egui::vec2((rect.width() - width) * phase, 0.0),
302 egui::vec2(width, rect.height()),
303 );
304 ui.painter().rect_filled(pulse, 8, BLUE);
305 response.widget_info(|| {
306 egui::WidgetInfo::labeled(
307 egui::Role::ProgressIndicator,
308 ui.is_enabled(),
309 "전체 크기를 알 수 없는 작업 진행 중",
310 )
311 });
312}
313
314pub const OPEN: &str = include_str!("assets/icons/open-archive.svg");
315pub const CREATE: &str = include_str!("assets/icons/create-zip.svg");
316pub const EXTRACT: &str = include_str!("assets/icons/extract-archive.svg");
317pub const UP: &str = include_str!("assets/icons/navigate-up.svg");
318pub const CLOSE: &str = include_str!("assets/icons/close-dialog.svg");
319
320pub fn svg_icon(painter: &egui::Painter, rect: egui::Rect, asset: &str, color: Color32) {
321 for element in asset.split("points=\"").skip(1) {
322 let points = element
323 .split('"')
324 .next()
325 .unwrap_or_default()
326 .split_whitespace()
327 .filter_map(|pair| {
328 let (x, y) = pair.split_once(',')?;
329 Some(
330 rect.min
331 + egui::vec2(x.parse::<f32>().ok()?, y.parse::<f32>().ok()?)
332 * (rect.width() / 24.0),
333 )
334 })
335 .collect::<Vec<_>>();
336 painter.add(egui::Shape::line(
337 points,
338 Stroke::new(rect.width() / 18.0, color),
339 ));
340 }
341}
342
343pub fn tool(
344 ui: &mut egui::Ui,
345 label: &str,
346 asset: &str,
347 enabled: bool,
348 blue: bool,
349) -> egui::Response {
350 let response = ui.add_enabled(
351 enabled,
352 egui::Button::new(
353 egui::RichText::new(label)
354 .size(18.0)
355 .color(Color32::TRANSPARENT),
356 )
357 .frame(false)
358 .min_size(egui::vec2(108.0, 86.0)),
359 );
360 let rect = response.rect;
361 let color = if enabled {
362 if blue { BLUE } else { TEXT }
363 } else {
364 MUTED
365 };
366 let icon_rect = egui::Rect::from_center_size(
367 egui::pos2(rect.center().x, rect.top() + 27.0),
368 egui::vec2(36.0, 36.0),
369 );
370 ui.painter()
371 .rect_filled(rect.shrink2(egui::vec2(1.0, 27.0)), 0, PANEL);
372 svg_icon(ui.painter(), icon_rect, asset, color);
373 ui.painter().text(
374 egui::pos2(rect.center().x, rect.bottom() - 15.0),
375 egui::Align2::CENTER_CENTER,
376 label,
377 FontId::proportional(18.0),
378 color,
379 );
380 focus(&response);
381 response
382}
383
384pub const SEARCH: &str = include_str!("assets/icons/search-files.svg");
385
386pub fn icon_button(ui: &mut egui::Ui, name: &str, asset: &str, enabled: bool) -> egui::Response {
387 let response = ui
388 .add_enabled(
389 enabled,
390 egui::Button::new("").min_size(egui::vec2(38.0, 36.0)),
391 )
392 .on_hover_text(name);
393 response.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, enabled, name));
394 svg_icon(
395 ui.painter(),
396 egui::Rect::from_center_size(response.rect.center(), egui::vec2(22.0, 22.0)),
397 asset,
398 if enabled { TEXT } else { MUTED },
399 );
400 focus(&response);
401 response
402}