team-ljyzipelo

공개
550줄20 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;
30/// 글자 크기 단계. 앱의 모든 글자는 이 세 값 중 하나를 씀.
31/// 기본 16px을 기준으로 보조 글자는 작게, 제목과 강조 글자는 크게 씀.
32pub const FONT_SMALL: f32 = 14.0;
33pub const FONT_BODY: f32 = 16.0;
34pub const FONT_LARGE: f32 = 20.0;
35/// 경로와 검색 입력란의 높이. 위쪽 경로 줄은 이 높이만큼만 차지함.
36pub const FIELD: f32 = 36.0;
37/// 테두리 없는 버튼에 마우스를 올리거나 누를 때의 배경.
38pub const HOVER: Color32 = Color32::from_rgb(229, 233, 239);
39pub const PRESS: Color32 = Color32::from_rgb(214, 220, 229);
40
41/// 앱에 포함한 기본 글꼴 (SIL OFL 1.1, assets/fonts/OFL.txt).
42pub const PRETENDARD: &[u8] = include_bytes!("assets/fonts/Pretendard-Regular.ttf");
43
44/// Pretendard를 기본 글꼴로 쓰고, 없는 글자는 운영체제 한글 글꼴로 대신 그림.
45pub fn fonts(fallback: Option<(Vec<u8>, u32)>) -> egui::FontDefinitions {
46 let mut fonts = egui::FontDefinitions::empty();
47 fonts.font_data.insert(
48 "pretendard".into(),
49 egui::FontData::from_static(PRETENDARD).into(),
50 );
51 let mut family = vec!["pretendard".to_owned()];
52 if let Some((bytes, index)) = fallback {
53 let mut data = egui::FontData::from_owned(bytes);
54 data.index = index;
55 fonts.font_data.insert("system-kr".into(), data.into());
56 family.push("system-kr".into());
57 }
58 fonts
59 .families
60 .insert(egui::FontFamily::Proportional, family.clone());
61 fonts.families.insert(egui::FontFamily::Monospace, family);
62 fonts
63}
64
65pub fn apply(ctx: &egui::Context) {
66 ctx.set_theme(egui::Theme::Light);
67 ctx.set_visuals(egui::Visuals::light());
68 ctx.style_mut_of(egui::Theme::Light, |style| {
69 style
70 .text_styles
71 .insert(TextStyle::Body, FontId::proportional(FONT_BODY));
72 style
73 .text_styles
74 .insert(TextStyle::Button, FontId::proportional(FONT_BODY));
75 style
76 .text_styles
77 .insert(TextStyle::Small, FontId::proportional(FONT_SMALL));
78 style
79 .text_styles
80 .insert(TextStyle::Heading, FontId::proportional(FONT_LARGE));
81 style.visuals.override_text_color = Some(TEXT);
82 style.visuals.panel_fill = BACKGROUND;
83 style.visuals.window_fill = PANEL;
84 style.visuals.extreme_bg_color = PANEL;
85 style.visuals.faint_bg_color = STRIPE;
86 style.visuals.selection.bg_fill = SELECTED;
87 style.visuals.selection.stroke = Stroke::new(2.0, BLUE);
88 style.visuals.widgets.inactive.bg_fill = PANEL;
89 style.visuals.widgets.inactive.weak_bg_fill = PANEL;
90 style.visuals.widgets.inactive.bg_stroke = Stroke::new(1.0, BORDER);
91 style.visuals.widgets.hovered.bg_fill = DROP;
92 style.visuals.widgets.hovered.weak_bg_fill = DROP;
93 style.visuals.widgets.hovered.bg_stroke = Stroke::new(1.0, BLUE);
94 style.visuals.widgets.active.bg_stroke = Stroke::new(2.0, BLUE);
95 for widget in [
96 &mut style.visuals.widgets.inactive,
97 &mut style.visuals.widgets.hovered,
98 &mut style.visuals.widgets.active,
99 &mut style.visuals.widgets.noninteractive,
100 ] {
101 widget.corner_radius = egui::CornerRadius::same(6);
102 widget.fg_stroke = Stroke::new(1.5, TEXT);
103 }
104 style.visuals.window_corner_radius = egui::CornerRadius::same(8);
105 style.spacing.item_spacing = egui::vec2(SPACE, SPACE);
106 style.spacing.button_padding = egui::vec2(16.0, SPACE);
107 style.spacing.interact_size.y = ROW;
108 style.spacing.window_margin = egui::Margin::same(PAD);
109 style.visuals.disabled_alpha = 0.4;
110 // 클릭과 표시가 즉시 바뀌도록 서서히 나타남, 부드러운 스크롤, 커서 깜빡임을 모두 끔.
111 style.animation_time = 0.0;
112 style.scroll_animation = egui::style::ScrollAnimation::none();
113 style.visuals.text_cursor.blink = false;
114 });
115}
116
117pub fn focus(response: &egui::Response) {
118 if response.has_focus() {
119 response.ctx.layer_painter(response.layer_id).rect_stroke(
120 response.rect.expand(2.0),
121 6,
122 Stroke::new(2.0, BLUE),
123 egui::StrokeKind::Outside,
124 );
125 }
126}
127
128pub fn action(ui: &mut egui::Ui, text: &str, primary: bool, enabled: bool) -> egui::Response {
129 let response = ui
130 .scope(|ui| {
131 if primary {
132 let style = ui.style_mut();
133 style.visuals.widgets.inactive.bg_fill = BLUE;
134 style.visuals.widgets.inactive.weak_bg_fill = BLUE;
135 style.visuals.widgets.hovered.bg_fill = BLUE_HOVER;
136 style.visuals.widgets.hovered.weak_bg_fill = BLUE_HOVER;
137 style.visuals.widgets.active.bg_fill = BLUE_ACTIVE;
138 style.visuals.widgets.active.weak_bg_fill = BLUE_ACTIVE;
139 }
140 ui.add_enabled(
141 enabled,
142 egui::Button::new(egui::RichText::new(text).strong().color(if primary {
143 PANEL
144 } else if text == "취소" {
145 ERROR
146 } else {
147 TEXT
148 }))
149 .min_size(egui::vec2(
150 if primary { 112.0 } else { 0.0 },
151 if primary { BUTTON } else { 36.0 },
152 ))
153 .stroke(Stroke::new(1.0, if primary { BLUE } else { BORDER })),
154 )
155 })
156 .inner;
157 focus(&response);
158 response
159}
160
161pub const STATUS_HEIGHT: f32 = 34.0;
162
163/// 경로 줄 높이를 꽉 채우는 입력란을 그림. 모서리는 각지게 하고,
164/// 위 테두리는 상단 메뉴의 구분선이 대신해 선이 겹쳐 보이지 않게 함. 초점이 있으면 파란 테두리.
165pub fn field_frame(ui: &egui::Ui, rect: egui::Rect, focused: bool) {
166 let painter = ui.painter();
167 painter.rect_filled(rect, 0, PANEL);
168 if focused {
169 painter.rect_stroke(rect, 0, Stroke::new(1.0, BLUE), egui::StrokeKind::Inside);
170 return;
171 }
172 let stroke = Stroke::new(1.0, BORDER);
173 painter.vline(rect.left() + 0.5, rect.y_range(), stroke);
174 painter.vline(rect.right() - 0.5, rect.y_range(), stroke);
175 painter.hline(rect.x_range(), rect.bottom() - 0.5, stroke);
176}
177
178/// 상태 줄 글자를 칸의 세로 가운데에 그림. 넘치면 줄이고 전체 글자는 툴팁으로 보여 줌.
179pub fn status_text(
180 ui: &mut egui::Ui,
181 rect: egui::Rect,
182 text: &str,
183 width: f32,
184 align: egui::Align,
185) {
186 let mut job = egui::text::LayoutJob::simple_singleline(
187 text.into(),
188 FontId::proportional(FONT_BODY),
189 TEXT,
190 );
191 job.wrap = egui::text::TextWrapping {
192 max_width: width.max(0.0),
193 max_rows: 1,
194 ..Default::default()
195 };
196 let galley = ui.fonts_mut(|fonts| fonts.layout_job(job));
197 let size = galley.size();
198 let x = if align == egui::Align::Max {
199 rect.right() - size.x
200 } else {
201 rect.left()
202 };
203 let text_rect = egui::Rect::from_min_size(egui::pos2(x, rect.center().y - size.y / 2.0), size);
204 ui.painter().galley(text_rect.min, galley, TEXT);
205 let response = ui.interact(
206 text_rect,
207 ui.scope_id()
208 .with(("status-text", align == egui::Align::Max)),
209 egui::Sense::hover(),
210 );
211 response.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Label, true, text));
212 response.on_hover_text(text);
213}
214
215pub fn panel(fill: Color32) -> egui::Frame {
216 egui::Frame::new()
217 .fill(fill)
218 .inner_margin(egui::Margin::same(PAD))
219 .corner_radius(8)
220}
221
222#[derive(Clone, Copy)]
223pub enum Icon {
224 File,
225 Folder,
226 Archive,
227 Lock,
228 Success,
229 Warning,
230 Error,
231}
232
233pub fn icon(ui: &mut egui::Ui, kind: Icon, size: f32) {
234 let (rect, _) = ui.allocate_exact_size(egui::vec2(size, size), egui::Sense::hover());
235 let p = ui.painter();
236 let color = match kind {
237 Icon::Folder => FOLDER,
238 Icon::Archive => BLUE,
239 Icon::Lock | Icon::Warning => WARNING,
240 Icon::Success => SUCCESS,
241 Icon::Error => ERROR,
242 Icon::File => FILE,
243 };
244 let s = Stroke::new(if size > 32.0 { 2.5 } else { 1.5 }, color);
245 let point = |x, y| rect.min + egui::vec2(x * size, y * size);
246 let box_at = |x, y, w, h| egui::Rect::from_min_max(point(x, y), point(x + w, y + h));
247 match kind {
248 Icon::Folder => {
249 p.rect_filled(box_at(0.08, 0.22, 0.42, 0.22), 2, color);
250 p.rect_filled(box_at(0.08, 0.34, 0.84, 0.50), 2, color);
251 }
252 Icon::File | Icon::Archive => {
253 p.rect_stroke(
254 box_at(0.19, 0.08, 0.62, 0.84),
255 2,
256 s,
257 egui::StrokeKind::Inside,
258 );
259 p.line_segment([point(0.60, 0.08), point(0.60, 0.30)], s);
260 p.line_segment([point(0.60, 0.30), point(0.81, 0.30)], s);
261 if matches!(kind, Icon::Archive) {
262 for y in [0.42, 0.57, 0.72] {
263 p.line_segment([point(0.39, y), point(0.59, y)], s);
264 }
265 }
266 }
267 Icon::Lock => {
268 p.circle_stroke(point(0.50, 0.35), size * 0.23, s);
269 p.rect_filled(box_at(0.18, 0.39, 0.64, 0.47), 2, color);
270 }
271 Icon::Success | Icon::Error => {
272 p.circle_stroke(rect.center(), size * 0.40, s);
273 if matches!(kind, Icon::Success) {
274 p.line_segment([point(0.28, 0.50), point(0.44, 0.66)], s);
275 p.line_segment([point(0.44, 0.66), point(0.74, 0.33)], s);
276 } else {
277 p.line_segment([point(0.34, 0.34), point(0.66, 0.66)], s);
278 p.line_segment([point(0.66, 0.34), point(0.34, 0.66)], s);
279 }
280 }
281 Icon::Warning => {
282 p.add(egui::Shape::closed_line(
283 vec![point(0.50, 0.08), point(0.94, 0.86), point(0.06, 0.86)],
284 s,
285 ));
286 p.line_segment([point(0.50, 0.32), point(0.50, 0.57)], s);
287 p.circle_filled(point(0.50, 0.73), size * 0.035, color);
288 }
289 }
290}
291
292pub fn bounded_text(ui: &mut egui::Ui, text: &str, width: f32) -> egui::Response {
293 bounded_text_hover(ui, text, text, width)
294}
295
296/// 폭에 맞춰 줄인 글자를 그리고, 마우스를 올리면 hover 글자를 보여 줌.
297pub fn bounded_text_hover(
298 ui: &mut egui::Ui,
299 text: &str,
300 hover: &str,
301 width: f32,
302) -> egui::Response {
303 let width = width.max(0.0);
304 ui.allocate_ui_with_layout(
305 egui::vec2(width, ui.text_style_height(&TextStyle::Body)),
306 ui.layout().clone(),
307 |ui| {
308 ui.set_min_width(width);
309 ui.set_max_width(width);
310 ui.add(egui::Label::new(text).truncate())
311 },
312 )
313 .inner
314 .on_hover_text(hover)
315}
316
317pub fn window_title(ui: &egui::Ui, text: &str, width: f32) -> String {
318 let mut job = egui::text::LayoutJob::simple_singleline(
319 text.into(),
320 FontId::proportional(FONT_BODY),
321 TEXT,
322 );
323 job.wrap = egui::text::TextWrapping {
324 max_width: width.max(0.0),
325 max_rows: 1,
326 break_anywhere: true,
327 ..Default::default()
328 };
329 ui.fonts_mut(|fonts| fonts.layout_job(job))
330 .rows
331 .iter()
332 .flat_map(|row| row.glyphs.iter().map(|glyph| glyph.chr))
333 .collect()
334}
335
336pub fn column_label(
337 ui: &mut egui::Ui,
338 text: impl Into<egui::WidgetText>,
339 width: f32,
340 align: egui::Align,
341) -> egui::Response {
342 let layout = if align == egui::Align::Max {
343 egui::Layout::right_to_left(egui::Align::Center)
344 } else {
345 egui::Layout::left_to_right(egui::Align::Center)
346 };
347 ui.allocate_ui_with_layout(egui::vec2(width, ROW), layout, |ui| {
348 ui.set_min_width(width);
349 ui.set_max_width(width);
350 ui.add(egui::Label::new(text).truncate())
351 })
352 .inner
353}
354
355pub fn action_pair(
356 ui: &mut egui::Ui,
357 secondary: &str,
358 primary: Option<(&str, bool)>,
359) -> (bool, bool) {
360 let text_width = |text: &str| {
361 ui.painter()
362 .layout_no_wrap(text.into(), FontId::proportional(FONT_BODY), TEXT)
363 .size()
364 .x
365 };
366 let width = text_width(secondary)
367 + 32.0
368 + primary.map_or(0.0, |(label, _)| {
369 SPACE + (text_width(label) + 32.0).max(112.0)
370 });
371 ui.horizontal(|ui| {
372 ui.with_layout(egui::Layout::left_to_right(egui::Align::Center), |ui| {
373 ui.add_space((ui.available_width() - width).max(0.0));
374 let secondary = action(ui, secondary, false, true).clicked();
375 let primary = primary.is_some_and(|(label, enabled)| {
376 action(ui, label, true, enabled)
377 .on_disabled_hover_text(match label {
378 "만들기" => "묶을 파일이나 폴더를 추가하세요",
379 "다른 폴더에 풀기" => "기존 풀 위치 폴더를 먼저 확인하세요",
380 _ => "입력한 내용을 확인하세요",
381 })
382 .clicked()
383 });
384 (secondary, primary)
385 })
386 .inner
387 })
388 .inner
389}
390
391pub fn indeterminate(ui: &mut egui::Ui) {
392 let (rect, response) = ui.allocate_exact_size(
393 egui::vec2(ui.available_width(), SPACE * 2.0),
394 egui::Sense::hover(),
395 );
396 // 움직이는 표시 없이 전체 막대를 옅은 파란색으로 채워 진행 중임을 나타냄.
397 ui.painter()
398 .rect_filled(rect, 8, Color32::from_rgb(191, 211, 248));
399 response.widget_info(|| {
400 egui::WidgetInfo::labeled(
401 egui::Role::ProgressIndicator,
402 ui.is_enabled(),
403 "전체 크기를 알 수 없는 작업 진행 중",
404 )
405 });
406}
407
408pub const OPEN: &str = include_str!("assets/icons/open-archive.svg");
409pub const CREATE: &str = include_str!("assets/icons/create-zip.svg");
410pub const EXTRACT: &str = include_str!("assets/icons/extract-archive.svg");
411pub const UP: &str = include_str!("assets/icons/navigate-up.svg");
412pub const CLOSE: &str = include_str!("assets/icons/close-dialog.svg");
413
414pub fn svg_icon(painter: &egui::Painter, rect: egui::Rect, asset: &str, color: Color32) {
415 for element in asset.split("points=\"").skip(1) {
416 let points = element
417 .split('"')
418 .next()
419 .unwrap_or_default()
420 .split_whitespace()
421 .filter_map(|pair| {
422 let (x, y) = pair.split_once(',')?;
423 Some(
424 rect.min
425 + egui::vec2(x.parse::<f32>().ok()?, y.parse::<f32>().ok()?)
426 * (rect.width() / 24.0),
427 )
428 })
429 .collect::<Vec<_>>();
430 let width = rect.width() / 18.0;
431 // 꼭짓점과 끝점에 원을 더해 둥근 이음과 둥근 끝을 만듦.
432 for point in &points {
433 painter.circle_filled(*point, width / 2.0, color);
434 }
435 painter.add(egui::Shape::line(points, Stroke::new(width, color)));
436 }
437}
438
439/// 가장 긴 이름(압축해제, FONT_SMALL에서 약 48px) 양옆에 12px씩 남는 폭.
440pub const TOOL_SIZE: egui::Vec2 = egui::vec2(72.0, 68.0);
441const TOOL_ICON: f32 = 30.0;
442const TOOL_LABEL: f32 = FONT_SMALL;
443
444pub fn tool(
445 ui: &mut egui::Ui,
446 label: &str,
447 asset: &str,
448 enabled: bool,
449 blue: bool,
450) -> egui::Response {
451 let (rect, response) = ui.allocate_exact_size(
452 TOOL_SIZE,
453 if enabled {
454 egui::Sense::click()
455 } else {
456 egui::Sense::hover()
457 },
458 );
459 response.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, enabled, label));
460 let color = if enabled {
461 if blue { BLUE } else { TEXT }
462 } else {
463 MUTED
464 };
465 if enabled && (response.hovered() || response.has_focus()) {
466 let fill = if response.is_pointer_button_down_on() {
467 PRESS
468 } else {
469 HOVER
470 };
471 ui.painter().rect_filled(rect, 8, fill);
472 }
473 // 아이콘과 글자를 붙여 가운데에 두고, 아래쪽은 여백으로 남김.
474 let label_height = TOOL_LABEL * 1.3;
475 let gap = 4.0;
476 let top = rect.top() + (rect.height() - TOOL_ICON - gap - label_height) / 2.0;
477 let icon_rect = egui::Rect::from_min_size(
478 egui::pos2(rect.center().x - TOOL_ICON / 2.0, top),
479 egui::vec2(TOOL_ICON, TOOL_ICON),
480 );
481 svg_icon(ui.painter(), icon_rect, asset, color);
482 ui.painter().text(
483 egui::pos2(
484 rect.center().x,
485 icon_rect.bottom() + gap + label_height / 2.0,
486 ),
487 egui::Align2::CENTER_CENTER,
488 label,
489 FontId::proportional(TOOL_LABEL),
490 color,
491 );
492 focus(&response);
493 response
494}
495
496/// 테두리와 배경 없이 아이콘만 그리는 버튼. 사용할 수 있을 때만 마우스를 올리면 배경이 어두워짐.
497pub fn flat_icon_button(
498 ui: &mut egui::Ui,
499 name: &str,
500 asset: &str,
501 enabled: bool,
502 size: egui::Vec2,
503) -> egui::Response {
504 let (rect, response) = ui.allocate_exact_size(
505 size,
506 if enabled {
507 egui::Sense::click()
508 } else {
509 egui::Sense::hover()
510 },
511 );
512 let response = response.on_hover_text(name);
513 response.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, enabled, name));
514 if enabled && response.hovered() {
515 let fill = if response.is_pointer_button_down_on() {
516 PRESS
517 } else {
518 HOVER
519 };
520 ui.painter().rect_filled(rect, 6, fill);
521 }
522 svg_icon(
523 ui.painter(),
524 egui::Rect::from_center_size(rect.center(), egui::vec2(20.0, 20.0)),
525 asset,
526 if enabled { TEXT } else { MUTED },
527 );
528 focus(&response);
529 response
530}
531
532pub const SEARCH: &str = include_str!("assets/icons/search-files.svg");
533
534pub fn icon_button(ui: &mut egui::Ui, name: &str, asset: &str, enabled: bool) -> egui::Response {
535 let response = ui
536 .add_enabled(
537 enabled,
538 egui::Button::new("").min_size(egui::vec2(38.0, 36.0)),
539 )
540 .on_hover_text(name);
541 response.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, enabled, name));
542 svg_icon(
543 ui.painter(),
544 egui::Rect::from_center_size(response.rect.center(), egui::vec2(22.0, 22.0)),
545 asset,
546 if enabled { TEXT } else { MUTED },
547 );
548 focus(&response);
549 response
550}