team-ljyzipelo

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