1#![allow(dead_code)]
12
13use crate::kernel::draw;
14use crate::os_lib::web_engine::{draw_border, WebEngine};
15use alloc::string::String;
16use alloc::vec::Vec;
17
18const CONTROL_BAR_H: i32 = 40;
21
22struct TabSlot {
28 title: String,
29 engine: Option<WebEngine>,
30 discarded: bool,
33 url: String,
35 last_active_ms: u64,
37}
38
39static SVG_BACK: &[u8] = b"<svg width=\"20\" height=\"20\" viewBox=\"0 0 20 20\" fill=\"none\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M12.5 16.5L6 10L12.5 3.5\" stroke=\"currentColor\" stroke-width=\"2.5\" stroke-linecap=\"round\" stroke-linejoin=\"round\"/></svg>";
40
41static SVG_FORWARD: &[u8] = b"<svg width=\"20\" height=\"20\" viewBox=\"0 0 20 20\" fill=\"none\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M7.5 3.5L14 10L7.5 16.5\" stroke=\"currentColor\" stroke-width=\"2.5\" stroke-linecap=\"round\" stroke-linejoin=\"round\"/></svg>";
42
43static SVG_HOME: &[u8] = b"<svg width=\"20\" height=\"20\" viewBox=\"0 0 20 20\" fill=\"none\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M10 2.5L3.5 8V16.5C3.5 17 4 17.5 4.5 17.5H7.5V12.5H12.5V17.5H15.5C16 17.5 16.5 17 16.5 16.5V8L10 2.5Z\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linejoin=\"round\"/></svg>";
44
45static SVG_SHIELD_SECURE: &[u8] = b"<svg width=\"20\" height=\"20\" viewBox=\"0 0 20 20\" fill=\"none\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M10 2.5C13 3.5 16.5 3.5 16.5 3.5V8.5C16.5 13 13 16.5 10 17.5C7 16.5 3.5 13 3.5 8.5V3.5C3.5 3.5 7 3.5 10 2.5Z\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linejoin=\"round\"/><path d=\"M7 10L9 12L13 8\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\"/></svg>";
46
47static SVG_SHIELD_WARN: &[u8] = b"<svg width=\"20\" height=\"20\" viewBox=\"0 0 20 20\" fill=\"none\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M10 2.5C13 3.5 16.5 3.5 16.5 3.5V8.5C16.5 13 13 16.5 10 17.5C7 16.5 3.5 13 3.5 8.5V3.5C3.5 3.5 7 3.5 10 2.5Z\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linejoin=\"round\"/><path d=\"M4.5 4.5L15.5 15.5\" stroke=\"currentColor\" stroke-width=\"2\"/></svg>";
48
49static SVG_RELOAD: &[u8] = b"<svg width=\"20\" height=\"20\" viewBox=\"0 0 20 20\" fill=\"none\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M17 10C17 13.866 13.866 17 10 17C6.13401 17 3 13.866 3 10C3 6.13401 6.13401 3 10 3C12.186 3 14.136 4.003 15.42 5.58M17 3V6H14\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\"/></svg>";
50
51fn render_home(engine: &mut WebEngine, top: i32) {
53 engine.current_is_https = false;
54 engine.load_html(crate::os_lib::web_engine::HOME_HTML, "home", top);
55 engine.title = String::from("AtmOS Browser");
56}
57
58pub struct WebBrowser {
60 pub engine: WebEngine,
62 pub address_bar_focused: bool,
63 pub address_box: crate::kernel::text_box::TextBox,
64 pub active_tab_idx: usize,
65 tabs: Vec<TabSlot>,
66 last_discard_check_ms: u64,
68 pub omnibox: crate::os_lib::web_engine::omnibox::OmniboxEngine,
69 pub omnibox_items: Vec<crate::os_lib::web_engine::omnibox::OmniboxItem>,
70 pub selected_suggestion: Option<usize>,
71}
72
73static REDRAW_LOADING: core::sync::atomic::AtomicUsize = core::sync::atomic::AtomicUsize::new(0);
76static REDRAW_DIRTY: core::sync::atomic::AtomicUsize = core::sync::atomic::AtomicUsize::new(0);
77static REDRAW_VIDEO: core::sync::atomic::AtomicUsize = core::sync::atomic::AtomicUsize::new(0);
78static REDRAW_IMG: core::sync::atomic::AtomicUsize = core::sync::atomic::AtomicUsize::new(0);
79
80pub fn log_redraw_reasons() {
82}
83
84impl WebBrowser {
85 pub fn execute_search_or_navigate(&mut self, text: &str) {
86 let input = text.trim();
87 if input.is_empty() {
88 return;
89 }
90 self.discard_if_needed(true);
92 let top = self.top_offset();
93 if let Some(rest) = input.strip_prefix('/') {
94 let loaded = {
95 let _fs_guard = crate::kernel::fs::FS_LOCK.lock();
96 crate::kernel::fs::get_fs().read_file_checked(input).ok().flatten()
97 };
98 if let Some((_meta, content)) = loaded {
99 if let Ok(html) = core::str::from_utf8(&content) {
100 self.engine.load_html(html, rest, top);
101 }
102 }
103 } else if let Some((host, path)) = crate::os_lib::web_engine::site_hooks::hooks().navigate_shortcut(input) {
104 self.engine.load_page(true, &host, &path, true, top);
106 } else if let Some(about) = crate::os_lib::web_engine::url_resolve::normalize_about(input) {
107 self.engine.load_page(false, &about, "", true, top);
109 } else if let Some((is_https, host, path)) =
110 crate::os_lib::web_engine::url_resolve::split_explicit_url(input)
111 {
112 self.engine.load_page(is_https, &host, &path, true, top);
113 } else if input.contains('.') && !input.contains(' ') {
114 let parts: Vec<&str> = input.splitn(2, '/').collect();
115 let host = parts[0];
116 let path = if parts.len() > 1 {
117 alloc::format!("/{}", parts[1])
118 } else {
119 String::from("/")
120 };
121 let is_https = !host.starts_with("localhost");
122 self.engine.load_page(is_https, host, &path, true, top);
123 } else {
124 if let Some((host, path)) = crate::os_lib::web_engine::site_hooks::hooks().default_search(input) {
126 self.engine.load_page(true, &host, &path, true, top);
127 }
128 }
129 self.address_bar_focused = false;
130 self.omnibox_items.clear();
131 self.selected_suggestion = None;
132 let final_url = self.engine.get_url();
133 self.omnibox.record_visit(&self.engine.title, &final_url);
134 self.address_box.set_text(&final_url);
135 self.engine.dirty = true;
136 self.engine.dirty_reason = "mod.rs:exec_nav";
137 }
138
139 pub fn top_offset(&self) -> i32 {
142 CONTROL_BAR_H
143 }
144
145 fn sync_active_title(&mut self) {
147 let title = crate::os_lib::web_engine::url_resolve::tab_label(
149 &self.engine.title,
150 &self.engine.get_url(),
151 );
152 if let Some(slot) = self.tabs.get_mut(self.active_tab_idx) {
153 slot.title = title;
154 }
155 }
156
157 fn activate(&mut self, idx: usize) {
160 if idx == self.active_tab_idx || idx >= self.tabs.len() {
161 return;
162 }
163 self.sync_active_title();
164 let now = crate::kernel::timer::get_system_time_ms();
165 self.tabs[self.active_tab_idx].last_active_ms = now;
167 if self.tabs[idx].discarded {
169 self.reload_discarded(idx);
170 return;
171 }
172 if let Some(mut next) = self.tabs[idx].engine.take() {
174 core::mem::swap(&mut self.engine, &mut next);
176 self.tabs[self.active_tab_idx].engine = Some(next);
178 self.active_tab_idx = idx;
179 }
180 self.engine.active = true;
181 self.engine.dirty = true;
182 self.engine.dirty_reason = "mod.rs:105";
183 self.address_box.set_text(&self.engine.get_url());
184 self.address_bar_focused = false;
185 }
186}
187
188impl WebBrowser {
189 fn blank_engine(&self) -> WebEngine {
191 let mut e = WebEngine::new();
192 e.win_w = self.engine.win_w;
193 e.win_h = self.engine.win_h;
194 e
195 }
196
197 fn reload_discarded(&mut self, idx: usize) {
200 let url = self.tabs[idx].url.clone();
201 let fresh = self.blank_engine();
202 let prev = core::mem::replace(&mut self.engine, fresh);
203 self.tabs[self.active_tab_idx].engine = Some(prev);
204 self.active_tab_idx = idx;
205 self.tabs[idx].discarded = false;
206 self.tabs[idx].url = String::new();
207 self.engine.active = true;
208 crate::warn!("[BROWSER] [DISCARD] 破棄していたタブ {} に切り替えたので読み直す: {}", idx, url);
209 self.execute_search_or_navigate(&url);
211 self.address_bar_focused = false;
212 }
213
214 pub fn discard_if_needed(&mut self, before_load: bool) {
219 use crate::kernel::tab_discard::{pick, TabInfo};
220 let (used, total) = crate::kernel::allocator::get_heap_stats();
221 let free = total.saturating_sub(used);
222 let app_bytes = crate::kernel::allocator::MEM_LEDGER
223 .bytes_of(crate::kernel::window_mgr::current_group())
224 .max(0) as usize;
225 let limit = crate::kernel::mem_quota::app_limit(total);
226 let infos: Vec<TabInfo> = self
227 .tabs
228 .iter()
229 .enumerate()
230 .map(|(i, t)| TabInfo {
231 active: i == self.active_tab_idx,
232 discarded: t.discarded,
233 worth: t.engine.as_ref().is_some_and(|e| {
234 let h = e.current_host.as_str();
235 !crate::os_lib::web_engine::url_resolve::is_about_host(h) && h != "localhost" && h != "newtab"
236 }),
237 last_active_ms: t.last_active_ms,
238 })
239 .collect();
240 let Some(idx) = pick(free, app_bytes, limit, &infos, before_load) else { return };
241 let Some(old) = self.tabs[idx].engine.take() else { return };
242 let url = old.get_url();
243 self.tabs[idx].discarded = true;
244 self.tabs[idx].url = url.clone();
245 drop(old);
247 let after = crate::kernel::allocator::get_heap_stats().0;
248 crate::warn!(
249 "[BROWSER] [DISCARD] 裏のタブ {} を破棄した({}): ヒープ {}MB → {}MB(空きは {}MB だった{})",
250 idx,
251 url,
252 used >> 20,
253 after >> 20,
254 free >> 20,
255 if before_load { "。読み込みの前" } else { "" }
256 );
257 }
258}
259
260impl Default for WebBrowser {
261 fn default() -> Self {
262 Self::new()
263 }
264}
265
266impl WebBrowser {
267 pub fn new() -> Self {
268 crate::os_lib::web_engine::clear_browser_cache();
270 let mut engine = WebEngine::new();
271 engine.win_w = 800;
272 engine.win_h = 600;
273 render_home(&mut engine, CONTROL_BAR_H);
275 let initial_url = engine.get_url();
276 let initial_slot = TabSlot {
278 title: String::from("AtmOS Browser"),
279 engine: None,
280 discarded: false,
281 url: String::new(),
282 last_active_ms: 0,
283 };
284 Self {
285 engine,
286 address_bar_focused: false,
287 address_box: crate::kernel::text_box::TextBox::with_text(&initial_url),
288 active_tab_idx: 0,
289 tabs: alloc::vec![initial_slot],
290 last_discard_check_ms: 0,
291 omnibox: crate::os_lib::web_engine::omnibox::OmniboxEngine::new(),
292 omnibox_items: Vec::new(),
293 selected_suggestion: None,
294 }
295 }
296}
297
298static mut GLOBAL_BROWSER: Option<WebBrowser> = None;
300
301pub fn init() {
303 unsafe {
304 GLOBAL_BROWSER = Some(WebBrowser::new());
305 }
306}
307
308pub fn get_instance() -> &'static mut WebBrowser {
309 unsafe { GLOBAL_BROWSER.get_or_insert_with(WebBrowser::new) }
311}
312
313pub fn is_active() -> bool {
314 unsafe {
315 match &GLOBAL_BROWSER {
316 Some(b) => b.engine.active,
317 None => false,
318 }
319 }
320}
321
322pub fn hovered_cursor_is_text() -> bool {
324 unsafe {
325 match &GLOBAL_BROWSER {
326 Some(b) => b.engine.active && b.engine.hovered_cursor_is_text,
327 None => false,
328 }
329 }
330}
331
332pub fn hovered_cursor() -> crate::kernel::draw::CursorKind {
334 unsafe {
335 match &GLOBAL_BROWSER {
336 Some(b) if b.engine.active => {
337 if b.engine.hovered_cursor_is_text {
338 crate::kernel::draw::CursorKind::Text
339 } else if !b.engine.hovered_cursor_kind.is_empty() {
340 crate::kernel::draw::CursorKind::from_css(&b.engine.hovered_cursor_kind)
341 } else {
342 crate::kernel::draw::CursorKind::Default
343 }
344 }
345 _ => crate::kernel::draw::CursorKind::Default,
346 }
347 }
348}
349
350pub fn needs_render() -> bool {
351 unsafe {
352 match &GLOBAL_BROWSER {
353 Some(b) => {
354 let r = b.engine.active && (b.engine.loading || b.engine.dirty);
359 if r {
360 if b.engine.loading {
361 RENDER_LOADING.fetch_add(1, core::sync::atomic::Ordering::Relaxed);
362 }
363 if b.engine.dirty {
364 RENDER_DIRTY.fetch_add(1, core::sync::atomic::Ordering::Relaxed);
365 }
366 }
367 r
368 }
369 None => false,
370 }
371 }
372}
373
374static RENDER_LOADING: core::sync::atomic::AtomicUsize = core::sync::atomic::AtomicUsize::new(0);
376static RENDER_DIRTY: core::sync::atomic::AtomicUsize = core::sync::atomic::AtomicUsize::new(0);
377
378impl crate::kernel::window_mgr::App for WebBrowser {
380 fn name(&self) -> &str {
381 "browser"
382 }
383
384 fn instance_info(&self) -> alloc::string::String {
385 crate::os_lib::web_engine::url_resolve::tab_label(&self.engine.title, &self.engine.get_url())
387 }
388
389 fn live_title(&self) -> Option<alloc::string::String> {
390 Some(alloc::format!("Browser - {}", self.engine.get_url()))
391 }
392
393 fn on_resize(&mut self, width: u32, height: u32) {
394 let top = self.top_offset();
395 self.engine.win_w = width;
396 self.engine.win_h = height;
397 let html = self.engine.last_html.clone();
398 self.engine.parse_and_layout(&html, None, top, width, height);
399 let viewport_h = height.saturating_sub(top as u32);
400 self.engine.js_runtime.fire_resize(width as i32, viewport_h as i32);
401 self.engine.dirty = true;
402 self.engine.dirty_reason = "mod.rs:202";
403 }
404
405 fn needs_timer_redraw(&self) -> bool {
406 let loading = self.engine.loading;
412 let dirty = self.engine.dirty;
413 let video = self.engine.video_playing;
414 let img = crate::os_lib::web_engine::image_results_ready();
415 let canvas = crate::os_lib::canvas2d::canvas_dirty();
418 let font_wait = crate::os_lib::web_engine::font_pending() > 0;
432 if loading {
433 REDRAW_LOADING.fetch_add(1, core::sync::atomic::Ordering::Relaxed);
434 }
435 if dirty {
436 REDRAW_DIRTY.fetch_add(1, core::sync::atomic::Ordering::Relaxed);
437 }
439 if video {
440 REDRAW_VIDEO.fetch_add(1, core::sync::atomic::Ordering::Relaxed);
441 }
442 if img {
443 REDRAW_IMG.fetch_add(1, core::sync::atomic::Ordering::Relaxed);
444 }
445 let showing_placeholder = loading && self.engine.elements.is_empty();
459 let timers = self.engine.js_runtime.has_due_timers();
461 let frames = self.engine.frames_want_redraw() || self.engine.has_pending_dynamic_scripts();
463 let dom_pending = self.engine.js_runtime.dom().try_borrow().is_ok_and(|d| d.dirty);
465 showing_placeholder || dirty || video || img || canvas || font_wait || timers || frames || dom_pending
466 }
467
468 fn draw(
470 &mut self,
471 screen: &crate::kernel::draw::Screen,
472 win_x: u32,
473 win_y: u32,
474 win_w: u32,
475 win_h: u32,
476 ) {
477 crate::os_lib::web_engine::set_draw_stage(1);
478 let _ = crate::os_lib::canvas2d::take_canvas_dirty();
481 let top = self.top_offset();
482 if self.engine.win_w != win_w || self.engine.win_h != win_h || self.engine.top_offset != top
484 {
485 let size_changed = self.engine.win_w != win_w || self.engine.win_h != win_h;
487 self.engine.win_w = win_w;
488 self.engine.win_h = win_h;
489 let html = self.engine.last_html.clone();
490 self.engine.parse_and_layout(&html, None, top, win_w, win_h);
491 if size_changed {
493 let viewport_h = win_h.saturating_sub(top as u32);
494 self.engine
495 .js_runtime
496 .fire_resize(win_w as i32, viewport_h as i32);
497 }
498 }
499 let theme = crate::kernel::config::get_config().theme;
500 self.engine.draw(screen, win_x, win_y, win_w, win_h, top);
510
511 screen.boxfill(
513 win_x,
514 win_y,
515 win_x + win_w,
516 win_y + CONTROL_BAR_H as u32,
517 crate::kernel::draw::Color(theme.tab_area_bg),
518 );
519
520 let can_back = self.engine.can_go_back();
522 let back_fg = if can_back { theme.button_fg } else { theme.non_active_window_border };
523 screen.boxfill(
524 win_x + 10,
525 win_y + 8,
526 win_x + 42,
527 win_y + 32,
528 draw::Color(theme.button_bg),
529 );
530 draw_border(
531 screen,
532 (win_x + 10) as i32,
533 (win_y + 8) as i32,
534 (win_x + 42) as i32,
535 (win_y + 32) as i32,
536 theme.non_active_window_border,
537 );
538 screen.draw_svg_icon(win_x + 16, win_y + 10, SVG_BACK, 20, draw::Color(back_fg));
539
540 let can_fwd = self.engine.can_go_forward();
542 let fwd_fg = if can_fwd { theme.button_fg } else { theme.non_active_window_border };
543 screen.boxfill(
544 win_x + 48,
545 win_y + 8,
546 win_x + 80,
547 win_y + 32,
548 draw::Color(theme.button_bg),
549 );
550 draw_border(
551 screen,
552 (win_x + 48) as i32,
553 (win_y + 8) as i32,
554 (win_x + 80) as i32,
555 (win_y + 32) as i32,
556 theme.non_active_window_border,
557 );
558 screen.draw_svg_icon(win_x + 54, win_y + 10, SVG_FORWARD, 20, draw::Color(fwd_fg));
559
560 screen.boxfill(
562 win_x + 86,
563 win_y + 8,
564 win_x + 118,
565 win_y + 32,
566 draw::Color(theme.button_bg),
567 );
568 draw_border(
569 screen,
570 (win_x + 86) as i32,
571 (win_y + 8) as i32,
572 (win_x + 118) as i32,
573 (win_y + 32) as i32,
574 theme.non_active_window_border,
575 );
576 screen.draw_svg_icon(
577 win_x + 92,
578 win_y + 10,
579 SVG_HOME,
580 20,
581 draw::Color(theme.button_fg),
582 );
583
584 screen.boxfill(
586 win_x + 124,
587 win_y + 8,
588 win_x + 156,
589 win_y + 32,
590 draw::Color(theme.button_bg),
591 );
592 draw_border(
593 screen,
594 (win_x + 124) as i32,
595 (win_y + 8) as i32,
596 (win_x + 156) as i32,
597 (win_y + 32) as i32,
598 theme.non_active_window_border,
599 );
600 screen.draw_svg_icon(
601 win_x + 130,
602 win_y + 10,
603 SVG_RELOAD,
604 20,
605 draw::Color(theme.button_fg),
606 );
607
608 let is_about = crate::os_lib::web_engine::url_resolve::is_about_host(&self.engine.current_host);
611 let is_https = self.engine.current_is_https || is_about;
612 let badge_w = if is_https { 95 } else { 125 };
613 let badge_bg = if is_https { 0xFF1B4D3E } else { 0xFF4C1D1D };
614 let badge_border = if is_https { 0xFF50FA7B } else { 0xFFFF5555 };
615 let badge_lbl = if is_about { "Built-in" } else if is_https { "Secure" } else { "Not Secure" };
616 let badge_fg = if is_https { 0xFFECFDF5 } else { 0xFFFEE2E2 };
617 let badge_svg = if is_https {
618 SVG_SHIELD_SECURE
619 } else {
620 SVG_SHIELD_WARN
621 };
622
623 screen.boxfill(
624 win_x + 162,
625 win_y + 8,
626 win_x + 162 + badge_w,
627 win_y + 32,
628 draw::Color(badge_bg),
629 );
630 draw_border(
631 screen,
632 (win_x + 162) as i32,
633 (win_y + 8) as i32,
634 (win_x + 162 + badge_w) as i32,
635 (win_y + 32) as i32,
636 badge_border,
637 );
638 screen.draw_svg_icon(
639 win_x + 164,
640 win_y + 10,
641 badge_svg,
642 20,
643 draw::Color(badge_border),
644 );
645 screen.draw_string_vector(
646 win_x + 189,
647 win_y + 13,
648 badge_lbl,
649 draw::Color(badge_fg),
650 11,
651 );
652
653 let badge_end = 162 + badge_w;
654
655 let bar_right = (win_x + win_w).saturating_sub(10);
657 let bar_left = win_x + badge_end + 6;
658 if bar_right > bar_left {
659 let addr_bg = if self.address_bar_focused {
660 theme.terminal_bg
661 } else {
662 theme.tab_area_bg
663 };
664 let addr_border = if self.address_bar_focused {
665 theme.focus_highlight
666 } else {
667 theme.non_active_window_border
668 };
669 screen.boxfill(
670 bar_left,
671 win_y + 8,
672 bar_right,
673 win_y + 32,
674 draw::Color(addr_bg),
675 );
676 draw_border(
677 screen,
678 bar_left as i32,
679 (win_y + 8) as i32,
680 bar_right as i32,
681 (win_y + 32) as i32,
682 addr_border,
683 );
684
685 let is_placeholder = self.address_box.text.is_empty() && !self.address_bar_focused;
686 let display_text = if is_placeholder {
687 String::from("Search or enter address...")
688 } else {
689 self.address_box.text.clone()
690 };
691 let text_color = if is_placeholder {
692 theme.non_active_window_border
693 } else {
694 theme.terminal_fg
695 };
696 screen.draw_string_vector(
697 bar_left + 10,
698 win_y + 12,
699 &display_text,
700 draw::Color(text_color),
701 12,
702 );
703
704 if self.address_bar_focused {
706 let prefix: String = self
707 .address_box
708 .text
709 .chars()
710 .take(self.address_box.cursor_pos)
711 .collect();
712 let pw = crate::kernel::vector_font::get_vector_string_width(&prefix, 12);
713 let cursor_x = bar_left + 10 + pw;
714 if cursor_x < bar_right {
715 screen.boxfill(
716 cursor_x,
717 win_y + 11,
718 cursor_x + 1,
719 win_y + 28,
720 draw::Color(crate::kernel::ime::cursor_color()),
721 );
722 }
723 }
724
725 let cur_url = self.engine.get_url();
727 let is_bm = self.omnibox.is_bookmarked(&cur_url);
728 let star_char = if is_bm { "*" } else { "o" };
729 let star_color = if is_bm { 0xFFFFD700 } else { theme.non_active_window_border };
730 screen.draw_string_vector(
731 bar_right.saturating_sub(20),
732 win_y + 13,
733 star_char,
734 draw::Color(star_color),
735 12,
736 );
737 }
738
739 screen.boxfill(
740 win_x,
741 win_y + 39,
742 win_x + win_w,
743 win_y + 40,
744 crate::kernel::draw::Color(theme.tab_area_border),
745 );
746
747 if self.address_bar_focused && !self.omnibox_items.is_empty() {
749 let badge_w = if self.engine.current_is_https { 95 } else { 125 };
750 let bar_left = win_x + 162 + badge_w + 6;
751 let bar_right = (win_x + win_w).saturating_sub(10);
752 if bar_right > bar_left {
753 let drop_x = bar_left;
754 let drop_w = bar_right - bar_left;
755 let item_h = 24u32;
756 let drop_h = (self.omnibox_items.len() as u32) * item_h + 4;
757 let drop_y = win_y + 34;
758
759 screen.boxfill_alpha(
761 drop_x,
762 drop_y,
763 drop_x + drop_w,
764 drop_y + drop_h,
765 draw::Color(0xFF1E1E28),
766 248,
767 );
768 draw_border(
769 screen,
770 drop_x as i32,
771 drop_y as i32,
772 (drop_x + drop_w) as i32,
773 (drop_y + drop_h) as i32,
774 0xFF3D3D4E,
775 );
776
777 for (i, item) in self.omnibox_items.iter().enumerate() {
778 let iy = drop_y + 2 + (i as u32) * item_h;
779 let is_selected = self.selected_suggestion == Some(i);
780
781 if is_selected {
782 screen.boxfill(
783 drop_x + 1,
784 iy,
785 drop_x + drop_w - 1,
786 iy + item_h,
787 draw::Color(0xFF353548),
788 );
789 }
790
791 let (icon_sym, icon_col) = match item.kind {
793 crate::os_lib::web_engine::omnibox::OmniboxItemKind::Bookmark => ("*", 0xFFFFD700),
794 crate::os_lib::web_engine::omnibox::OmniboxItemKind::History => (">", 0xFF6BA4E8),
795 crate::os_lib::web_engine::omnibox::OmniboxItemKind::Search => ("?", 0xFFDE5833),
796 };
797
798 screen.draw_string_vector(
799 drop_x + 8,
800 iy + 5,
801 icon_sym,
802 draw::Color(icon_col),
803 11,
804 );
805
806 let text_col = if is_selected { 0xFFFFFFFF } else { 0xFFD0D0E0 };
808 screen.draw_string_vector(
809 drop_x + 22,
810 iy + 5,
811 &item.title,
812 draw::Color(text_col),
813 12,
814 );
815
816 let badge_text = item.kind.badge_label();
818 if drop_w > 140 {
819 screen.draw_string_vector(
820 drop_x + drop_w - 85,
821 iy + 6,
822 badge_text,
823 draw::Color(icon_col),
824 10,
825 );
826 }
827 }
828 }
829 }
830
831 self.engine.sync_url_from_script();
834 if !self.address_bar_focused {
835 let url = self.engine.get_url();
836 if self.engine.dirty || self.address_box.text != url {
837 self.address_box.set_text(&url);
838 }
839 }
840 if !self.engine.title.is_empty() {
841 let label = crate::os_lib::web_engine::url_resolve::tab_label(
842 &self.engine.title,
843 &self.engine.get_url(),
844 );
845 if let Some(slot) = self.tabs.get_mut(self.active_tab_idx) {
846 slot.title = label;
847 }
848 }
849 crate::os_lib::web_engine::set_draw_stage(0);
850 }
851
852 fn on_mouse(
853 &mut self,
854 local_x: i32,
855 local_y: i32,
856 btn_left: bool,
857 btn_right: bool,
858 wheel: i32,
859 ) {
860 let top = self.top_offset();
861
862 if btn_left && self.address_bar_focused && !self.omnibox_items.is_empty() {
863 let bar_left = 162 + if self.engine.current_is_https { 95 } else { 125 } + 6;
864 let bar_right = (self.engine.win_w as i32).saturating_sub(10).max(bar_left + 100);
865 let drop_y = 34;
866 let item_h = 24;
867 let drop_h = (self.omnibox_items.len() as i32) * item_h + 4;
868 if (bar_left..=bar_right).contains(&local_x) && (drop_y..=drop_y + drop_h).contains(&local_y) {
869 let clicked_idx = ((local_y - drop_y - 2) / item_h) as usize;
870 if let Some(item) = self.omnibox_items.get(clicked_idx).cloned() {
871 self.execute_search_or_navigate(&item.url);
872 return;
873 }
874 }
875 }
876
877 if btn_left && (0..CONTROL_BAR_H).contains(&local_y) {
878 if (8..=32).contains(&local_y) {
879 if (10..=42).contains(&local_x) {
880 self.engine.go_back(top);
881 self.address_bar_focused = false;
882 self.address_box.set_text(&self.engine.get_url());
883 self.engine.dirty = true;
884 self.engine.dirty_reason = "mod.rs:532";
885 return;
886 }
887 if (48..=80).contains(&local_x) {
888 self.engine.go_forward(top);
889 self.address_bar_focused = false;
890 self.address_box.set_text(&self.engine.get_url());
891 self.engine.dirty = true;
892 self.engine.dirty_reason = "mod.rs:539";
893 return;
894 }
895 if (86..=118).contains(&local_x) {
896 render_home(&mut self.engine, top);
898 self.address_bar_focused = false;
899 self.address_box.set_text(&self.engine.get_url());
900 self.engine.dirty = true;
901 self.engine.dirty_reason = "mod.rs:547";
902 return;
903 }
904 if (124..=156).contains(&local_x) {
905 if self.engine.current_host == "localhost" {
907 let path = self.engine.current_path.clone();
908 if path == "/home" {
909 render_home(&mut self.engine, top);
911 } else {
912 let loaded = {
916 let _fs_guard = crate::kernel::fs::FS_LOCK.lock();
917 crate::kernel::fs::get_fs().read_file_checked(&path).ok().flatten()
918 };
919 if let Some((_meta, content)) = loaded {
920 if let Ok(html) = core::str::from_utf8(&content) {
921 self.engine.load_html(
922 html,
923 path.strip_prefix('/').unwrap_or(&path),
924 top,
925 );
926 }
927 }
928 }
929 } else {
930 self.engine.load_page(
931 self.engine.current_is_https,
932 &self.engine.current_host.clone(),
933 &self.engine.current_path.clone(),
934 false,
935 top,
936 );
937 }
938 self.address_bar_focused = false;
939 self.address_box.set_text(&self.engine.get_url());
940 return;
941 }
942 let badge_end = 162
944 + if self.engine.current_is_https {
945 95
946 } else {
947 125
948 };
949 let bar_left = badge_end + 6;
950 let bar_right = (self.engine.win_w as i32).saturating_sub(10).max(bar_left + 100);
951 if local_x >= bar_right - 26 && local_x <= bar_right {
952 let cur_url = self.engine.get_url();
953 let title = if self.engine.title.is_empty() { "Untitled" } else { &self.engine.title };
954 self.omnibox.toggle_bookmark(title, &cur_url);
955 self.engine.dirty = true;
956 self.engine.dirty_reason = "bookmark_toggle";
957 return;
958 }
959 if local_x >= bar_left {
960 self.address_bar_focused = true;
961 self.address_box.set_text(&self.engine.get_url());
962 self.engine.dirty = true;
963 self.engine.dirty_reason = "mod.rs:593";
964 return;
965 }
966 }
967 self.address_bar_focused = false;
968 self.engine.dirty = true;
969 self.engine.dirty_reason = "mod.rs:598";
970 return;
971 }
972
973 if btn_left {
974 self.address_bar_focused = false;
975 self.engine.dirty = true;
976 self.engine.dirty_reason = "mod.rs:604";
977 }
978
979 self.engine
980 .on_mouse(local_x, local_y, btn_left, btn_right, wheel, top);
981 }
982
983 fn on_key(&mut self, keycode: u8, pressed: bool, ascii: Option<char>) {
984 if !pressed {
985 return;
986 }
987
988 if self.address_bar_focused {
989 if keycode == 0x1C {
990 let chosen_url = if let Some(idx) = self.selected_suggestion {
992 self.omnibox_items.get(idx).map(|item| item.url.clone())
993 } else {
994 None
995 };
996 let text_to_nav = chosen_url.unwrap_or_else(|| self.address_box.text.clone());
997 self.execute_search_or_navigate(&text_to_nav);
998 } else if keycode == 0x50 {
999 if !self.omnibox_items.is_empty() {
1001 let next = match self.selected_suggestion {
1002 Some(i) => (i + 1) % self.omnibox_items.len(),
1003 None => 0,
1004 };
1005 self.selected_suggestion = Some(next);
1006 if let Some(item) = self.omnibox_items.get(next) {
1007 self.address_box.set_text(&item.title);
1008 }
1009 self.engine.dirty = true;
1010 self.engine.dirty_reason = "mod.rs:sugg_down";
1011 }
1012 } else if keycode == 0x48 {
1013 if !self.omnibox_items.is_empty() {
1015 let prev = match self.selected_suggestion {
1016 Some(0) | None => self.omnibox_items.len().saturating_sub(1),
1017 Some(i) => i - 1,
1018 };
1019 self.selected_suggestion = Some(prev);
1020 if let Some(item) = self.omnibox_items.get(prev) {
1021 self.address_box.set_text(&item.title);
1022 }
1023 self.engine.dirty = true;
1024 self.engine.dirty_reason = "mod.rs:sugg_up";
1025 }
1026 } else if keycode == 0x01 {
1027 self.omnibox_items.clear();
1029 self.selected_suggestion = None;
1030 self.engine.dirty = true;
1031 self.engine.dirty_reason = "mod.rs:sugg_esc";
1032 } else {
1033 let changed = self.address_box.handle_key(keycode, ascii);
1034 if changed {
1035 self.selected_suggestion = None;
1036 let text = self.address_box.text.trim();
1037 if !text.is_empty() {
1038 let search_cands = if !text.starts_with("http://") && !text.starts_with("https://") && !text.starts_with('/') {
1039 crate::os_lib::web_engine::site_hooks::hooks().search_suggestions(text)
1040 } else {
1041 alloc::vec![]
1042 };
1043 self.omnibox_items = self.omnibox.get_suggestions(text, &search_cands, 6);
1044 } else {
1045 self.omnibox_items.clear();
1046 }
1047 self.engine.dirty = true;
1048 self.engine.dirty_reason = "mod.rs:text_input";
1049 }
1050 }
1051 return;
1052 }
1053
1054 self.engine.on_key(keycode, pressed, ascii);
1055
1056 if (ascii == Some('\x03') || keycode == 0x2E) && self.engine.focused_id.is_none() && !self.address_bar_focused {
1058 let selected = self.engine.get_selected_text();
1059 if !selected.is_empty() {
1060 crate::os_lib::clipboard::set_text(&selected);
1061 crate::info!("[BROWSER] Copied {} bytes to clipboard: {:?}", selected.len(), selected);
1062 }
1063 }
1064
1065 if keycode == 0x01 && !self.address_bar_focused {
1067 self.engine.clear_selection();
1068 }
1069
1070 if ascii == Some('\x04') && self.engine.focused_id.is_none() && !self.address_bar_focused {
1072 let cur_url = self.engine.get_url();
1073 let title = if self.engine.title.is_empty() { "Untitled" } else { &self.engine.title };
1074 let added = self.omnibox.toggle_bookmark(title, &cur_url);
1075 crate::info!("[BROWSER] Bookmark {}: {} ({})", if added { "Added" } else { "Removed" }, title, cur_url);
1076 self.engine.dirty = true;
1077 self.engine.dirty_reason = "bookmark_toggle_shortcut";
1078 }
1079
1080 if keycode == 0x0E && self.engine.focused_id.is_none() {
1081 let top = self.top_offset();
1082 self.engine.go_back(top);
1083 self.address_box.set_text(&self.engine.get_url());
1084 }
1085
1086 let scroll_key = matches!(keycode, 0x48 | 0x50 | 0x49 | 0x51 | 0x47 | 0x4F);
1096 if scroll_key && (self.engine.focus_is_text_entry() || self.address_bar_focused) {
1097 crate::warn!(
1098 "[SCROLL] key={:#x} ignored: focus={:?} address_bar={}",
1099 keycode,
1100 self.engine.focused_id,
1101 self.address_bar_focused
1102 );
1103 }
1104 if !self.engine.focus_is_text_entry() && !self.address_bar_focused {
1105 let page = (self.engine.win_h as i32 - self.top_offset()).max(80);
1107 let dy = match keycode {
1108 0x48 => Some(-60), 0x50 => Some(60), 0x49 => Some(-page), 0x51 => Some(page), 0x47 => Some(-self.engine.max_scroll_y), 0x4F => Some(self.engine.max_scroll_y), _ => None,
1115 };
1116 if let Some(dy) = dy {
1117 crate::warn!("[SCROLL] key={:#x} dy={} max={}", keycode, dy, self.engine.max_scroll_y);
1118 self.engine.scroll(dy);
1119 self.engine.dirty = true;
1120 self.engine.dirty_reason = "browser/mod.rs:key_scroll";
1121 }
1122 }
1123 }
1124
1125 fn tabs(&self) -> alloc::vec::Vec<alloc::string::String> {
1126 self.tabs
1128 .iter()
1129 .enumerate()
1130 .map(|(i, t)| {
1131 if i == self.active_tab_idx {
1132 crate::os_lib::web_engine::url_resolve::tab_label(
1133 &self.engine.title,
1134 &self.engine.get_url(),
1135 )
1136 } else if t.discarded {
1137 alloc::format!("(zz) {}", t.title)
1139 } else {
1140 t.title.clone()
1141 }
1142 })
1143 .collect()
1144 }
1145
1146 fn active_tab(&self) -> usize {
1147 self.active_tab_idx
1148 }
1149
1150 fn close_tab(&mut self, idx: usize) -> bool {
1151 if idx >= self.tabs.len() {
1152 return false;
1153 }
1154
1155 let was_active = idx == self.active_tab_idx;
1156 self.tabs.remove(idx);
1157
1158 if self.tabs.is_empty() {
1159 self.engine.active = false;
1160 crate::os_lib::web_engine::clear_browser_cache();
1161 return true;
1162 }
1163
1164 if was_active {
1165 let new_idx = idx.min(self.tabs.len() - 1);
1167 if let Some(mut next) = self.tabs[new_idx].engine.take() {
1168 core::mem::swap(&mut self.engine, &mut next);
1169 }
1171 self.active_tab_idx = new_idx;
1172 if self.tabs[new_idx].discarded {
1174 let url = core::mem::take(&mut self.tabs[new_idx].url);
1175 self.tabs[new_idx].discarded = false;
1176 self.engine = self.blank_engine();
1177 self.engine.active = true;
1178 crate::warn!("[BROWSER] [DISCARD] 破棄していたタブ {} が表示になったので読み直す: {}", new_idx, url);
1179 self.execute_search_or_navigate(&url);
1180 return false;
1181 }
1182 self.engine.active = true;
1183 self.engine.dirty = true;
1184 self.engine.dirty_reason = "mod.rs:733";
1185 self.address_box.set_text(&self.engine.get_url());
1186 self.address_bar_focused = false;
1187 } else if idx < self.active_tab_idx {
1188 self.active_tab_idx -= 1;
1190 }
1191 false
1192 }
1193
1194 fn switch_tab(&mut self, idx: usize) {
1195 self.activate(idx);
1196 }
1197
1198 fn memory_checkpoint(&mut self) {
1201 if self.tabs.len() <= 1 {
1202 return;
1203 }
1204 let now = crate::kernel::timer::get_system_time_ms();
1205 if now.saturating_sub(self.last_discard_check_ms) < 250 {
1206 return;
1207 }
1208 self.last_discard_check_ms = now;
1209 self.discard_if_needed(false);
1210 }
1211
1212 fn open_url_in_new_tab(&mut self, url: &str) {
1214 if self.tabs.len() < 16 {
1215 self.add_tab();
1216 self.address_bar_focused = false;
1217 } else {
1218 crate::warn!("[BROWSER] タブが上限なので、今のタブで開く");
1219 }
1220 self.execute_search_or_navigate(url);
1221 }
1222
1223 fn add_tab(&mut self) {
1224 if self.tabs.len() >= 16 {
1225 return;
1226 }
1227 self.sync_active_title();
1229 let old_active = self.active_tab_idx;
1230 self.tabs[old_active].last_active_ms = crate::kernel::timer::get_system_time_ms();
1231 let w = self.engine.win_w;
1232 let h = self.engine.win_h;
1233
1234 let mut new_engine = WebEngine::new();
1236 new_engine.win_w = w;
1237 new_engine.win_h = h;
1238 new_engine.current_host = String::from("newtab");
1239 new_engine.current_path = String::from("/");
1240 new_engine.title = String::from("New Tab");
1241 new_engine.active = true;
1242 let top = self.top_offset();
1243 new_engine.parse_and_layout(
1244 "<h1>New Tab</h1><p>Enter URL in the address bar.</p>",
1245 None,
1246 top,
1247 w,
1248 h,
1249 );
1250
1251 let prev = core::mem::replace(&mut self.engine, new_engine);
1253 self.tabs[old_active].engine = Some(prev);
1254
1255 self.tabs.push(TabSlot {
1256 title: String::from("New Tab"),
1257 engine: None,
1258 discarded: false,
1259 url: String::new(),
1260 last_active_ms: 0,
1261 });
1262 self.active_tab_idx = self.tabs.len() - 1;
1263
1264 self.address_box.set_text("");
1265 self.address_bar_focused = true;
1266 self.engine.dirty = true;
1267 self.engine.dirty_reason = "mod.rs:786";
1268 }
1269
1270 fn get_dom_value(&self, id: &str) -> Option<alloc::string::String> {
1271 self.engine.form_values.get(id).cloned()
1272 }
1273
1274 fn set_dom_value(&mut self, id: &str, val: &str) {
1275 self.engine.form_values.insert(
1276 alloc::string::String::from(id),
1277 alloc::string::String::from(val),
1278 );
1279 self.engine.dirty = true;
1280 self.engine.dirty_reason = "mod.rs:798";
1281 }
1282
1283 fn get_dom_content(&self, id: &str) -> Option<alloc::string::String> {
1284 self.engine.dom_contents.get(id).cloned()
1285 }
1286
1287 fn set_dom_content(&mut self, id: &str, val: &str) {
1288 self.engine.set_dom_content(id, val);
1289 }
1290}