navigate.go RawHistoryFolder
1package usecase
2
3import (
4 "context"
5 "fmt"
6 "math"
7
8 "github.com/bnema/dumber/internal/application/port"
9 "github.com/bnema/dumber/internal/domain/entity"
10 "github.com/bnema/dumber/internal/logging"
11)
12
13// NavigateUseCase handles URL navigation with zoom application.
14type NavigateUseCase struct {
15 defaultZoom float64
16}
17
18// NewNavigateUseCase creates a new navigation use case.
19// defaultZoom is the zoom level to use when no per-domain zoom is saved (typically from config).
20func NewNavigateUseCase(defaultZoom float64) *NavigateUseCase {
21 if defaultZoom <= 0 || math.IsNaN(defaultZoom) || math.IsInf(defaultZoom, 0) {
22 defaultZoom = entity.ZoomDefault
23 }
24
25 return &NavigateUseCase{defaultZoom: defaultZoom}
26}
27
28// NavigateInput contains parameters for navigation.
29type NavigateInput struct {
30 URL string
31 PaneID string
32 WebView port.PageNavigator
33}
34
35// NavigateOutput contains the result of navigation.
36type NavigateOutput struct {
37 AppliedZoom float64
38}
39
40// Execute navigates to a URL.
41// Zoom is applied later via LoadCommitted event in ContentCoordinator.
42func (uc *NavigateUseCase) Execute(ctx context.Context, input NavigateInput) (*NavigateOutput, error) {
43 log := logging.FromContext(ctx)
44 log.Debug().
45 Str("url", logging.RedactURL(input.URL)).
46 Str("pane_id", input.PaneID).
47 Msg("navigating to URL")
48
49 // Navigate to URL (zoom will be applied on LoadCommitted to avoid shift)
50 // History is recorded on LoadCommitted when URI is guaranteed correct
51 if err := input.WebView.LoadURI(ctx, input.URL); err != nil {
52 return nil, fmt.Errorf("failed to load URL: %w", err)
53 }
54
55 log.Info().
56 Str("url", logging.RedactURL(input.URL)).
57 Msg("navigation initiated")
58
59 return &NavigateOutput{
60 AppliedZoom: uc.defaultZoom,
61 }, nil
62}
63
64// Reload reloads the current page.
65func (uc *NavigateUseCase) Reload(ctx context.Context, webview port.PageNavigator, bypassCache bool) error {
66 log := logging.FromContext(ctx).With().Float64("default_zoom", uc.defaultZoom).Logger()
67 log.Debug().Bool("bypass_cache", bypassCache).Msg("reloading page")
68
69 if bypassCache {
70 return webview.ReloadBypassCache(ctx)
71 }
72 return webview.Reload(ctx)
73}
74
75// Stop stops the current page load.
76func (uc *NavigateUseCase) Stop(ctx context.Context, webview port.PageNavigator) error {
77 log := logging.FromContext(ctx).With().Float64("default_zoom", uc.defaultZoom).Logger()
78 log.Debug().Msg("stopping page load")
79 return webview.Stop(ctx)
80}