feat: a browser page in the right sidebar, and icon tabs
- the right sidebar gains a Browser tab: address bar (back/forward, URL, go, stop/reload, open in the system browser), history, quick links for localhost:3000/5173/8080, and an embedded WebView on Android/iOS with an error bar and retry; desktop shows a mobile-only hint - Android allows cleartext traffic so local dev servers over HTTP load in the WebView - the tab row is now icons with tooltips (Files, Project knowledge, Terminal, Browser), so four tabs fit a phone without truncated labels; the tabs are kept alive offstage, so switching away and back keeps the browser page and terminal state - webview_flutter 4.14.1; version 0.6.7+607; tests cover the tab icons and the desktop fallback
This commit is contained in:
1 parent
fedc4dd79c
commit
ce944751b4
10 files changed
+563
-24
No files matched your search
@@ -631,4 +631,17 @@ const Map<String, String> chineseStrings = {
|
||||
'Saved to Photos': '已保存到相册',
|
||||
"Couldn't save the picture": '无法保存图片',
|
||||
'Storage permission is required to save the picture': '保存图片需要存储权限',
|
||||
|
||||
// browser
|
||||
'Browser': '浏览器',
|
||||
'Enter a URL': '输入网址',
|
||||
'Web browser': '网页浏览器',
|
||||
'Enter a URL above to start browsing': '在上方输入网址开始浏览',
|
||||
'Browser is only available on Android and iOS': '浏览器面板仅支持 Android 和 iOS',
|
||||
'Open in system browser': '在系统浏览器中打开',
|
||||
'Forward': '前进',
|
||||
'Reload': '重新加载',
|
||||
'History': '历史记录',
|
||||
'Invalid URL': '无效的网址',
|
||||
'Could not open the browser: {0}': '无法打开浏览器:{0}',
|
||||
};
|
||||
@@ -0,0 +1,343 @@
|
||||
// The right sidebar's web browser: an address bar over an embedded WebView,
|
||||
// with history navigation, reload/stop and opening the page in the system
|
||||
// browser. The panel keeps its page while another tab is shown (the tab stack
|
||||
// keeps it alive).
|
||||
import 'dart:io' show Platform;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:webview_flutter/webview_flutter.dart';
|
||||
|
||||
import '../l10n.dart';
|
||||
|
||||
class BrowserPanel extends StatefulWidget {
|
||||
const BrowserPanel({super.key});
|
||||
|
||||
@override
|
||||
State<BrowserPanel> createState() => _BrowserPanelState();
|
||||
}
|
||||
|
||||
class _BrowserPanelState extends State<BrowserPanel> {
|
||||
static const MethodChannel _channel = MethodChannel('openchamber/browser');
|
||||
static const _quickLinks = [
|
||||
'http://localhost:3000',
|
||||
'http://localhost:5173',
|
||||
'http://localhost:8080',
|
||||
];
|
||||
|
||||
final TextEditingController _url = TextEditingController();
|
||||
final List<String> _history = [];
|
||||
WebViewController? _web;
|
||||
bool _loading = false;
|
||||
bool _hasPage = false;
|
||||
bool _canBack = false;
|
||||
bool _canForward = false;
|
||||
int _progress = 0;
|
||||
String? _error;
|
||||
|
||||
bool get _supported => Platform.isAndroid || Platform.isIOS;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
if (!_supported) return;
|
||||
_web = WebViewController()
|
||||
..setJavaScriptMode(JavaScriptMode.unrestricted)
|
||||
..setBackgroundColor(Colors.transparent)
|
||||
..setNavigationDelegate(NavigationDelegate(
|
||||
onPageStarted: (url) {
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_loading = true;
|
||||
_hasPage = true;
|
||||
_progress = 0;
|
||||
_error = null;
|
||||
_setUrl(url);
|
||||
});
|
||||
_syncNavigation();
|
||||
},
|
||||
onPageFinished: (url) {
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_loading = false;
|
||||
_progress = 100;
|
||||
_setUrl(url);
|
||||
});
|
||||
_remember(url);
|
||||
_syncNavigation();
|
||||
},
|
||||
onProgress: (value) {
|
||||
if (mounted) setState(() => _progress = value);
|
||||
},
|
||||
onUrlChange: (change) {
|
||||
final url = change.url;
|
||||
if (url != null && mounted) setState(() => _setUrl(url));
|
||||
},
|
||||
onWebResourceError: (error) {
|
||||
if ((error.isForMainFrame ?? true) && mounted) {
|
||||
setState(() => _error = error.description);
|
||||
}
|
||||
},
|
||||
));
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_url.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _syncNavigation() async {
|
||||
final web = _web;
|
||||
if (web == null) return;
|
||||
final back = await web.canGoBack();
|
||||
final forward = await web.canGoForward();
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_canBack = back;
|
||||
_canForward = forward;
|
||||
});
|
||||
}
|
||||
|
||||
/// Puts [url] in the address field with the caret at its end, so the part
|
||||
/// that was just visited stays visible in the narrow bar.
|
||||
void _setUrl(String url) {
|
||||
_url.value = TextEditingValue(
|
||||
text: url,
|
||||
selection: TextSelection.collapsed(offset: url.length),
|
||||
);
|
||||
}
|
||||
|
||||
void _remember(String url) {
|
||||
if (url.isEmpty) return;
|
||||
_history.remove(url);
|
||||
_history.insert(0, url);
|
||||
if (_history.length > 20) _history.removeLast();
|
||||
}
|
||||
|
||||
void _navigate(String raw) {
|
||||
var text = raw.trim();
|
||||
if (text.isEmpty) return;
|
||||
if (!text.contains('://')) {
|
||||
final host = text.split('/').first.split(':').first;
|
||||
final local = host == 'localhost' ||
|
||||
host == '127.0.0.1' ||
|
||||
host == '[::1]' ||
|
||||
host == '0.0.0.0';
|
||||
text = '${local ? 'http' : 'https'}://$text';
|
||||
}
|
||||
final uri = Uri.tryParse(text);
|
||||
if (uri == null || !uri.hasAuthority) {
|
||||
_snack(tr('Invalid URL'));
|
||||
return;
|
||||
}
|
||||
FocusScope.of(context).unfocus();
|
||||
_setUrl(text);
|
||||
_web?.loadRequest(uri);
|
||||
}
|
||||
|
||||
Future<void> _openExternal() async {
|
||||
if (!Platform.isAndroid) return;
|
||||
final url = _url.text.trim();
|
||||
if (url.isEmpty) return;
|
||||
try {
|
||||
await _channel.invokeMethod<bool>('openUrl', {'url': url});
|
||||
} catch (e) {
|
||||
if (mounted) _snack(trArgs('Could not open the browser: {0}', [e]));
|
||||
}
|
||||
}
|
||||
|
||||
void _snack(String text) {
|
||||
ScaffoldMessenger.of(context)
|
||||
..hideCurrentSnackBar()
|
||||
..showSnackBar(SnackBar(content: AppText(text)));
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
if (!_supported) {
|
||||
return Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(32),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(Icons.public_off, size: 40, color: theme.hintColor),
|
||||
const SizedBox(height: 12),
|
||||
const AppText(
|
||||
'Browser is only available on Android and iOS',
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(fontSize: 13),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
_addressBar(theme),
|
||||
if (_loading)
|
||||
LinearProgressIndicator(
|
||||
value: _progress > 0 && _progress < 100 ? _progress / 100 : null,
|
||||
minHeight: 2,
|
||||
),
|
||||
if (_error != null) _errorBar(theme),
|
||||
Expanded(
|
||||
// Keep the WebView attached even before the first address: a
|
||||
// loadRequest on a controller with no platform view is lost. The
|
||||
// empty state is an overlay until a page starts.
|
||||
child: Stack(
|
||||
children: [
|
||||
Positioned.fill(child: WebViewWidget(controller: _web!)),
|
||||
if (!_hasPage)
|
||||
Positioned.fill(
|
||||
child: ColoredBox(
|
||||
color: theme.colorScheme.surface,
|
||||
child: _empty(theme),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _addressBar(ThemeData theme) {
|
||||
final web = _web;
|
||||
return Padding(
|
||||
padding: const EdgeInsets.fromLTRB(4, 6, 4, 6),
|
||||
child: Row(
|
||||
children: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.arrow_back, size: 20),
|
||||
tooltip: tr('Back'),
|
||||
onPressed: _canBack ? () => web?.goBack() : null,
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.arrow_forward, size: 20),
|
||||
tooltip: tr('Forward'),
|
||||
onPressed: _canForward ? () => web?.goForward() : null,
|
||||
),
|
||||
Expanded(
|
||||
child: TextField(
|
||||
controller: _url,
|
||||
keyboardType: TextInputType.url,
|
||||
textInputAction: TextInputAction.go,
|
||||
onSubmitted: _navigate,
|
||||
decoration: InputDecoration(
|
||||
isDense: true,
|
||||
hintText: tr('Enter a URL'),
|
||||
prefixIcon: const Icon(Icons.public, size: 18),
|
||||
suffixIcon: IconButton(
|
||||
icon: const Icon(Icons.arrow_forward, size: 18),
|
||||
tooltip: tr('Go'),
|
||||
onPressed: () => _navigate(_url.text),
|
||||
),
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(10)),
|
||||
),
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
icon: Icon(_loading ? Icons.stop : Icons.refresh, size: 20),
|
||||
tooltip: _loading ? tr('Stop') : tr('Reload'),
|
||||
onPressed: _loading
|
||||
? () => web?.runJavaScript('window.stop()')
|
||||
: web?.reload,
|
||||
),
|
||||
if (Platform.isAndroid)
|
||||
IconButton(
|
||||
icon: const Icon(Icons.open_in_browser, size: 20),
|
||||
tooltip: tr('Open in system browser'),
|
||||
onPressed: _openExternal,
|
||||
),
|
||||
if (_history.isNotEmpty)
|
||||
PopupMenuButton<String>(
|
||||
icon: const Icon(Icons.history, size: 20),
|
||||
tooltip: tr('History'),
|
||||
onSelected: (url) {
|
||||
_setUrl(url);
|
||||
_navigate(url);
|
||||
},
|
||||
itemBuilder: (_) => [
|
||||
for (final url in _history)
|
||||
PopupMenuItem(
|
||||
value: url,
|
||||
child: Text(url,
|
||||
maxLines: 1, overflow: TextOverflow.ellipsis),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _errorBar(ThemeData theme) {
|
||||
return Container(
|
||||
color: theme.colorScheme.errorContainer.withValues(alpha: 0.4),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(Icons.error_outline,
|
||||
size: 16, color: theme.colorScheme.error),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Text(
|
||||
_error!,
|
||||
maxLines: 2,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: TextStyle(fontSize: 12, color: theme.colorScheme.error),
|
||||
),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => _web?.reload(),
|
||||
child: const AppText('Retry'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _empty(ThemeData theme) {
|
||||
return Center(
|
||||
child: SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(Icons.public, size: 48, color: theme.hintColor),
|
||||
const SizedBox(height: 12),
|
||||
const AppText(
|
||||
'Web browser',
|
||||
style: TextStyle(fontSize: 18, fontWeight: FontWeight.w600),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
const AppText(
|
||||
'Enter a URL above to start browsing',
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(fontSize: 12.5),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 8,
|
||||
alignment: WrapAlignment.center,
|
||||
children: [
|
||||
for (final url in _quickLinks)
|
||||
ActionChip(
|
||||
label: Text(url),
|
||||
onPressed: () => _navigate(url),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import '../api/models.dart';
|
||||
import '../api/transport.dart';
|
||||
import '../state.dart';
|
||||
import '../widgets/markdown_view.dart';
|
||||
import 'browser_panel.dart';
|
||||
import 'knowledge_panel.dart';
|
||||
import 'terminal_panel.dart';
|
||||
import '../l10n.dart';
|
||||
@@ -110,6 +111,10 @@ class _FilesPanelState extends State<FilesPanel> {
|
||||
|
||||
int _tab = 0;
|
||||
|
||||
/// Tabs built at least once. Kept alive (offstage) so the browser, terminal
|
||||
/// and knowledge pages keep their state when another tab is shown.
|
||||
final Set<int> _built = {0};
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
@@ -426,7 +431,20 @@ class _FilesPanelState extends State<FilesPanel> {
|
||||
children: [
|
||||
_tabs(theme),
|
||||
const Divider(height: 1),
|
||||
Expanded(child: _body(theme)),
|
||||
Expanded(
|
||||
child: Stack(
|
||||
children: [
|
||||
for (var i = 0; i < 4; i++)
|
||||
if (_built.contains(i))
|
||||
Positioned.fill(
|
||||
child: Offstage(
|
||||
offstage: _tab != i,
|
||||
child: _tabBody(i, theme),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
@@ -436,7 +454,14 @@ class _FilesPanelState extends State<FilesPanel> {
|
||||
/// The tab row is the panel's title now; the only global action left is
|
||||
/// closing the panel. Everything else belongs to the tab's own header.
|
||||
Widget _tabs(ThemeData theme) {
|
||||
const labels = ['Files', 'Project knowledge', 'Terminal'];
|
||||
// Icons only: four labels no longer fit a phone's panel width. The
|
||||
// tooltips keep them identifiable.
|
||||
const tabs = [
|
||||
(Icons.folder_outlined, 'Files'),
|
||||
(Icons.menu_book_outlined, 'Project knowledge'),
|
||||
(Icons.terminal, 'Terminal'),
|
||||
(Icons.public, 'Browser'),
|
||||
];
|
||||
return Padding(
|
||||
padding: const EdgeInsets.fromLTRB(12, 6, 2, 6),
|
||||
child: Row(
|
||||
@@ -449,11 +474,20 @@ class _FilesPanelState extends State<FilesPanel> {
|
||||
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
|
||||
),
|
||||
segments: [
|
||||
for (var i = 0; i < labels.length; i++)
|
||||
ButtonSegment(value: i, label: AppText(labels[i])),
|
||||
for (var i = 0; i < tabs.length; i++)
|
||||
ButtonSegment(
|
||||
value: i,
|
||||
icon: Tooltip(
|
||||
message: tr(tabs[i].$2),
|
||||
child: Icon(tabs[i].$1, size: 19),
|
||||
),
|
||||
),
|
||||
],
|
||||
selected: {_tab},
|
||||
onSelectionChanged: (v) => setState(() => _tab = v.first),
|
||||
onSelectionChanged: (v) => setState(() {
|
||||
_tab = v.first;
|
||||
_built.add(v.first);
|
||||
}),
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
@@ -466,15 +500,17 @@ class _FilesPanelState extends State<FilesPanel> {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _body(ThemeData theme) {
|
||||
Widget _tabBody(int index, ThemeData theme) {
|
||||
if (_root.isEmpty) {
|
||||
return const Center(child: AppText('This session has no directory'));
|
||||
}
|
||||
switch (_tab) {
|
||||
switch (index) {
|
||||
case 1:
|
||||
return KnowledgePanel(app: widget.app, directory: _root);
|
||||
case 2:
|
||||
return TerminalPanel(app: widget.app, directory: _root);
|
||||
case 3:
|
||||
return const BrowserPanel();
|
||||
default:
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
|
||||
Reference in new issue
Block a user