navigation.go RawHistoryFolder
1package control
2
3import (
4 "context"
5 "log"
6 "os"
7
8 "github.com/bnema/dumber/pkg/webkit"
9 "github.com/bnema/dumber/services"
10)
11
12// NavigationController manages navigation functionality
13type NavigationController struct {
14 parserService *services.ParserService
15 browserService *services.BrowserService
16 webView *webkit.WebView
17 zoomController *ZoomController
18}
19
20// NewNavigationController creates a new navigation controller
21func NewNavigationController(
22 parserService *services.ParserService,
23 browserService *services.BrowserService,
24 webView *webkit.WebView,
25 zoomController *ZoomController,
26) *NavigationController {
27 return &NavigationController{
28 parserService: parserService,
29 browserService: browserService,
30 webView: webView,
31 zoomController: zoomController,
32 }
33}
34
35// NavigateToURL parses input and navigates to the resulting URL
36func (n *NavigationController) NavigateToURL(input string) error {
37 ctx := context.Background()
38 result, err := n.parserService.ParseInput(ctx, input)
39 if err != nil {
40 return err
41 }
42
43 // Record navigation in browser service
44 if _, navErr := n.browserService.Navigate(ctx, result.URL); navErr != nil {
45 log.Printf("Warning: failed to navigate to %s: %v", result.URL, navErr)
46 }
47
48 // Load URL in WebView
49 if err := n.webView.LoadURL(result.URL); err != nil {
50 return err
51 }
52
53 // Apply zoom for the new URL
54 n.zoomController.ApplyZoomForURL(result.URL)
55
56 return nil
57}
58
59// HandleBrowseCommand processes the browse command line argument
60func (n *NavigationController) HandleBrowseCommand() {
61 if len(os.Args) >= 3 && os.Args[1] == "browse" {
62 log.Printf("Browse command detected: %s", os.Args[2])
63 ctx := context.Background()
64 result, err := n.parserService.ParseInput(ctx, os.Args[2])
65 if err == nil {
66 log.Printf("Parsed input → URL: %s", result.URL)
67 if _, navErr := n.browserService.Navigate(ctx, result.URL); navErr != nil {
68 log.Printf("Warning: failed to navigate to %s: %v", result.URL, navErr)
69 }
70 if n.webView != nil {
71 log.Printf("Loading URL in WebView: %s", result.URL)
72 if err := n.webView.LoadURL(result.URL); err != nil {
73 log.Printf("Warning: failed to load URL: %v", err)
74 }
75 n.zoomController.ApplyZoomForURL(result.URL)
76 }
77 }
78 }
79}