Matthias Nott
2026-03-22 45c33f8cd35d4fe7679973f573fd6ecab9c2dd32
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:vibration/vibration.dart';
import '../providers/providers.dart';
import '../theme/app_theme.dart';
/// Terminal navigation screen with screenshot display and key grid.
class NavigateScreen extends ConsumerStatefulWidget {
  const NavigateScreen({super.key});
  @override
  ConsumerState<NavigateScreen> createState() => _NavigateScreenState();
}
class _NavigateScreenState extends ConsumerState<NavigateScreen> {
  @override
  Widget build(BuildContext context) {
    final screenshot = ref.watch(latestScreenshotProvider);
    final isDark = Theme.of(context).brightness == Brightness.dark;
    return Scaffold(
      appBar: AppBar(
        title: const Text('Navigate'),
        actions: [
          IconButton(
            icon: const Icon(Icons.refresh),
            onPressed: _requestScreenshot,
            tooltip: 'Refresh screenshot',
          ),
        ],
      ),
      body: Column(
        children: [
          // Screenshot display
          Expanded(
            child: screenshot != null
                ? Padding(
                    padding: const EdgeInsets.all(8),
                    child: InteractiveViewer(
                      minScale: 1.0,
                      maxScale: 3.0,
                      child: Image.memory(
                        base64Decode(
                          screenshot.contains(',')
                              ? screenshot.split(',').last
                              : screenshot,
                        ),
                        fit: BoxFit.contain,
                        errorBuilder: (_, e, st) => const Center(
                          child: Text('Screenshot decode error'),
                        ),
                      ),
                    ),
                  )
                : Center(
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        Icon(
                          Icons.screenshot_monitor,
                          size: 48,
                          color: isDark
                              ? AppColors.darkTextTertiary
                              : Colors.grey.shade400,
                        ),
                        const SizedBox(height: 12),
                        Text(
                          'No screenshot yet',
                          style: TextStyle(
                            color: isDark
                                ? AppColors.darkTextTertiary
                                : Colors.grey.shade500,
                          ),
                        ),
                        const SizedBox(height: 8),
                        TextButton(
                          onPressed: _requestScreenshot,
                          child: const Text('Request Screenshot'),
                        ),
                      ],
                    ),
                  ),
          ),
          // Key grid
          Container(
            padding: const EdgeInsets.all(12),
            decoration: BoxDecoration(
              color: isDark ? AppColors.darkSurface : AppColors.lightSurface,
              border: Border(
                top: BorderSide(
                  color: isDark ? Colors.white10 : Colors.black12,
                ),
              ),
            ),
            child: SafeArea(
              top: false,
              child: _buildKeyGrid(context),
            ),
          ),
        ],
      ),
    );
  }
  Widget _buildKeyGrid(BuildContext context) {
    return Column(
      children: [
        // Row 1: 0, Up, G
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            _keyButton('0', '0'),
            _keyButton('\u2191', 'k', label: 'k'),
            _keyButton('G', 'G'),
          ],
        ),
        const SizedBox(height: 8),
        // Row 2: Left, Down, Right
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            _keyButton('\u2190', 'h', label: 'h'),
            _keyButton('\u2193', 'j', label: 'j'),
            _keyButton('\u2192', 'l', label: 'l'),
          ],
        ),
        const SizedBox(height: 8),
        // Row 3: dd, Esc, Tab
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            _keyButton('dd', 'dd'),
            _keyButton('Esc', 'Escape'),
            _keyButton('Tab', 'Tab'),
          ],
        ),
        const SizedBox(height: 8),
        // Row 4: Enter (wide), ^C
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            Expanded(
              flex: 2,
              child: Padding(
                padding: const EdgeInsets.symmetric(horizontal: 4),
                child: _keyButton('Enter', 'Return', wide: true),
              ),
            ),
            Expanded(
              child: Padding(
                padding: const EdgeInsets.symmetric(horizontal: 4),
                child: _keyButton('^C', 'ctrl+c'),
              ),
            ),
          ],
        ),
      ],
    );
  }
  Widget _keyButton(String display, String key,
      {String? label, bool wide = false}) {
    final isDark = Theme.of(context).brightness == Brightness.dark;
    return SizedBox(
      width: wide ? null : 72,
      height: 44,
      child: Material(
        color: isDark ? AppColors.darkInputBg : AppColors.lightInputBg,
        borderRadius: BorderRadius.circular(8),
        child: InkWell(
          onTap: () => _sendKey(key),
          borderRadius: BorderRadius.circular(8),
          child: Center(
            child: Text(
              display,
              style: TextStyle(
                fontSize: 16,
                fontWeight: FontWeight.w600,
                color: isDark ? AppColors.darkTextPrimary : AppColors.lightTextPrimary,
              ),
            ),
          ),
        ),
      ),
    );
  }
  void _sendKey(String key) {
    _haptic();
    // Send via WebSocket - the chat screen's WS is in the provider
    // We need to access the WS through the provider system
    // For now, send a nav command message
    final activeSessionId = ref.read(activeSessionIdProvider);
    // Build the navigate command
    // This sends a key press to the AIBroker daemon
    // which forwards it to the active terminal session
    // The WS is managed by ChatScreen, so we'll use a message approach
    // Since we can't directly access the WS from here,
    // we send through the provider approach - the message will be picked up
    // by the WS service in ChatScreen via a shared notification mechanism.
    // For simplicity, we use a global event bus pattern.
    NavigateNotifier.instance?.sendKey(key, activeSessionId);
    // Request updated screenshot after key
    Future.delayed(const Duration(milliseconds: 500), _requestScreenshot);
  }
  void _requestScreenshot() {
    final activeSessionId = ref.read(activeSessionIdProvider);
    NavigateNotifier.instance?.requestScreenshot(activeSessionId);
  }
  Future<void> _haptic() async {
    try {
      final hasVibrator = await Vibration.hasVibrator();
      if (hasVibrator) {
        Vibration.vibrate(duration: 15);
      }
    } catch (_) {}
  }
}
/// Global notifier to bridge navigate screen to WebSocket.
/// Set by ChatScreen when WS is initialized.
class NavigateNotifier {
  static NavigateNotifier? instance;
  final void Function(String key, String? sessionId) sendKey;
  final void Function(String? sessionId) requestScreenshot;
  NavigateNotifier({
    required this.sendKey,
    required this.requestScreenshot,
  });
}