1
This commit is contained in:
1 parent
54c3083c24
commit
87e03ce0d8
44 files changed
+3965
-307
No files matched your search
@@ -0,0 +1,93 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import '../models/distro_info.dart';
|
||||
import '../services/distro_manager.dart';
|
||||
|
||||
/// Linux 发行版状态提供者(供 UI 监听与绑定)
|
||||
class DistroProvider with ChangeNotifier {
|
||||
final DistroManager _manager = DistroManager();
|
||||
|
||||
/// 各发行版状态缓存
|
||||
final Map<String, DistroStatus> _statuses = {};
|
||||
|
||||
/// 安装进度 (0.0 ~ 1.0)
|
||||
final Map<String, double> _progress = {};
|
||||
|
||||
/// 状态描述文字(如 "正在解压... 45%")
|
||||
final Map<String, String> _statusMessages = {};
|
||||
|
||||
/// 默认创建会话使用的发行版 ID
|
||||
String _defaultDistroId = 'alpine';
|
||||
|
||||
String get defaultDistroId => _defaultDistroId;
|
||||
|
||||
List<DistroInfo> get distros => _manager.listDistros();
|
||||
|
||||
DistroStatus getStatus(String id) => _statuses[id] ?? DistroStatus.notInstalled;
|
||||
|
||||
double getProgress(String id) => _progress[id] ?? 0.0;
|
||||
|
||||
String getStatusMessage(String id) => _statusMessages[id] ?? '';
|
||||
|
||||
/// 初始化检查所有发行版状态
|
||||
Future<void> checkAllStatuses() async {
|
||||
for (final distro in _manager.listDistros()) {
|
||||
try {
|
||||
final isInst = await _manager.isInstalled(distro.id);
|
||||
_statuses[distro.id] = isInst ? DistroStatus.installed : DistroStatus.notInstalled;
|
||||
} catch (_) {
|
||||
_statuses[distro.id] = DistroStatus.error;
|
||||
}
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// 设置默认发行版
|
||||
void setDefaultDistro(String id) {
|
||||
if (_defaultDistroId != id) {
|
||||
_defaultDistroId = id;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
/// 安装发行版
|
||||
Future<bool> installDistro(String distroId) async {
|
||||
_statuses[distroId] = DistroStatus.installing;
|
||||
_progress[distroId] = 0.0;
|
||||
_statusMessages[distroId] = '准备开始安装...';
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
await _manager.installDistro(
|
||||
distroId,
|
||||
onProgress: (prog, msg) {
|
||||
_progress[distroId] = prog;
|
||||
_statusMessages[distroId] = msg;
|
||||
notifyListeners();
|
||||
},
|
||||
);
|
||||
_statuses[distroId] = DistroStatus.installed;
|
||||
_statusMessages[distroId] = '已就绪';
|
||||
notifyListeners();
|
||||
return true;
|
||||
} catch (e) {
|
||||
_statuses[distroId] = DistroStatus.error;
|
||||
_statusMessages[distroId] = '安装失败: $e';
|
||||
notifyListeners();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// 卸载发行版
|
||||
Future<void> uninstallDistro(String distroId) async {
|
||||
try {
|
||||
await _manager.uninstallDistro(distroId);
|
||||
_statuses[distroId] = DistroStatus.notInstalled;
|
||||
_progress.remove(distroId);
|
||||
_statusMessages.remove(distroId);
|
||||
notifyListeners();
|
||||
} catch (e) {
|
||||
_statusMessages[distroId] = '卸载失败: $e';
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'dart:convert';
|
||||
import 'package:code_editor/models/app_font.dart';
|
||||
import 'package:code_editor/models/editor_theme.dart';
|
||||
import 'package:code_editor/models/virtual_keyboard_config.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
@@ -8,20 +9,30 @@ import 'package:shared_preferences/shared_preferences.dart';
|
||||
class SettingsProvider extends ChangeNotifier {
|
||||
//外观
|
||||
static const String _keyAppThemeMode = 'app_theme_mode'; //应用主题
|
||||
static const String _keyUiFont = 'app_ui_font'; //界面字体
|
||||
|
||||
//编辑区
|
||||
static const String _keyEditorTheme = 'editor_theme_id'; //代码主题
|
||||
static const String _keyEditorFontSize = 'editor_font_size'; //代码字体大小
|
||||
static const String _keyEditorIndentSize = 'editor_indent_size'; //缩进大小
|
||||
static const String _keyWordWrap = 'editor_word_wrap'; //自动换行
|
||||
static const String _keyEnableVirtualKeyboard = 'enable_virtual_keyboard'; //小键盘启用
|
||||
static const String _keyVirtualKeyboardConfig = 'virtual_keyboard_config'; //小键盘配置JSON
|
||||
static const String _keyEditorFont = 'editor_font_family'; //代码字体
|
||||
|
||||
//终端
|
||||
static const String _keyTerminalFont = 'terminal_font_family'; //终端字体
|
||||
|
||||
//语言
|
||||
static const String _keyAppLocale = 'app_locale';
|
||||
|
||||
ThemeMode _appThemeMode = ThemeMode.system;
|
||||
String _uiFontId = 'system_default';
|
||||
EditorTheme _editorTheme = EditorTheme.atomOneDark;
|
||||
String _editorFontId = 'monospace';
|
||||
String _terminalFontId = 'monospace';
|
||||
double _fontSize = 14.0;
|
||||
int _indentSize = 2;
|
||||
bool _wordWrap = true;
|
||||
bool _enableVirtualKeyboard = defaultTargetPlatform == TargetPlatform.android || defaultTargetPlatform == TargetPlatform.iOS;
|
||||
String _virtualKeyboardConfigJson = VirtualKeyboardConfig.defaultJsonPretty();
|
||||
@@ -29,8 +40,18 @@ class SettingsProvider extends ChangeNotifier {
|
||||
Locale? _locale;
|
||||
|
||||
ThemeMode get appThemeMode => _appThemeMode;
|
||||
String get uiFontId => _uiFontId;
|
||||
AppFontItem get uiFont => AppFonts.getUiFont(_uiFontId);
|
||||
|
||||
EditorTheme get editorTheme => _editorTheme;
|
||||
String get editorFontId => _editorFontId;
|
||||
AppFontItem get editorFont => AppFonts.getEditorFont(_editorFontId);
|
||||
|
||||
String get terminalFontId => _terminalFontId;
|
||||
AppFontItem get terminalFont => AppFonts.getTerminalFont(_terminalFontId);
|
||||
|
||||
double get fontSize => _fontSize;
|
||||
int get indentSize => _indentSize;
|
||||
bool get wordWrap => _wordWrap;
|
||||
bool get enableVirtualKeyboard => _enableVirtualKeyboard;
|
||||
String get virtualKeyboardConfigJson => _virtualKeyboardConfigJson;
|
||||
@@ -64,6 +85,11 @@ class SettingsProvider extends ChangeNotifier {
|
||||
_fontSize = savedFontSize;
|
||||
}
|
||||
|
||||
final savedIndentSize = prefs.getInt(_keyEditorIndentSize);
|
||||
if (savedIndentSize != null && (savedIndentSize == 2 || savedIndentSize == 4 || savedIndentSize == 8)) {
|
||||
_indentSize = savedIndentSize;
|
||||
}
|
||||
|
||||
final savedWordWrap = prefs.getBool(_keyWordWrap);
|
||||
if(savedWordWrap != null) {
|
||||
_wordWrap = savedWordWrap;
|
||||
@@ -86,6 +112,22 @@ class SettingsProvider extends ChangeNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
//设置字体
|
||||
final savedUiFont = prefs.getString(_keyUiFont);
|
||||
if (savedUiFont != null && savedUiFont.isNotEmpty) {
|
||||
_uiFontId = savedUiFont;
|
||||
}
|
||||
|
||||
final savedEditorFont = prefs.getString(_keyEditorFont);
|
||||
if (savedEditorFont != null && savedEditorFont.isNotEmpty) {
|
||||
_editorFontId = savedEditorFont;
|
||||
}
|
||||
|
||||
final savedTerminalFont = prefs.getString(_keyTerminalFont);
|
||||
if (savedTerminalFont != null && savedTerminalFont.isNotEmpty) {
|
||||
_terminalFontId = savedTerminalFont;
|
||||
}
|
||||
|
||||
//设置语言
|
||||
final savedLocale = prefs.getString(_keyAppLocale);
|
||||
if(savedLocale != null && savedLocale.isNotEmpty) {
|
||||
@@ -129,6 +171,17 @@ class SettingsProvider extends ChangeNotifier {
|
||||
}catch (_) {}
|
||||
}
|
||||
|
||||
Future<void> setIndentSize(int size) async {
|
||||
if (size != 2 && size != 4 && size != 8) return;
|
||||
if (_indentSize == size) return;
|
||||
_indentSize = size;
|
||||
notifyListeners();
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setInt(_keyEditorIndentSize, size);
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
Future<void> setWordWrap(bool wrap) async {
|
||||
if(_wordWrap == wrap) return;
|
||||
_wordWrap = wrap;
|
||||
@@ -190,4 +243,34 @@ class SettingsProvider extends ChangeNotifier {
|
||||
}
|
||||
}catch (_) {}
|
||||
}
|
||||
|
||||
Future<void> setUiFontId(String fontId) async {
|
||||
if (_uiFontId == fontId) return;
|
||||
_uiFontId = fontId;
|
||||
notifyListeners();
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setString(_keyUiFont, fontId);
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
Future<void> setEditorFontId(String fontId) async {
|
||||
if (_editorFontId == fontId) return;
|
||||
_editorFontId = fontId;
|
||||
notifyListeners();
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setString(_keyEditorFont, fontId);
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
Future<void> setTerminalFontId(String fontId) async {
|
||||
if (_terminalFontId == fontId) return;
|
||||
_terminalFontId = fontId;
|
||||
notifyListeners();
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setString(_keyTerminalFont, fontId);
|
||||
} catch (_) {}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,185 @@
|
||||
import 'package:code_editor/models/terminal_session.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// 全局终端会话管理 Provider
|
||||
class TerminalProvider extends ChangeNotifier {
|
||||
final List<TerminalSession> _sessions = [];
|
||||
int _activeIndex = 0;
|
||||
|
||||
List<TerminalSession> get sessions => List.unmodifiable(_sessions);
|
||||
|
||||
int get activeIndex => _activeIndex;
|
||||
|
||||
bool get isEmpty => _sessions.isEmpty;
|
||||
|
||||
bool get isNotEmpty => _sessions.isNotEmpty;
|
||||
|
||||
TerminalSession? get activeSession {
|
||||
if (_sessions.isEmpty) return null;
|
||||
if (_activeIndex >= 0 && _activeIndex < _sessions.length) {
|
||||
return _sessions[_activeIndex];
|
||||
}
|
||||
return _sessions.first;
|
||||
}
|
||||
|
||||
/// 懒加载:进入终端页面时调用,如果尚未创建会话,则自动创建并激活首个会话
|
||||
void ensureInitialized() {
|
||||
if (_sessions.isEmpty) {
|
||||
createSession(name: '会话', activate: true);
|
||||
}
|
||||
}
|
||||
|
||||
/// 创建新会话
|
||||
/// [distroId]: 发行版标识符(如 'alpine', 'host')
|
||||
/// [activate]: 是否自动跳转切换到该会话。抽屉右上角加号点击时为 false(直接添加但不跳转)。
|
||||
TerminalSession createSession({
|
||||
String name = '会话',
|
||||
String distroId = 'alpine',
|
||||
bool activate = false,
|
||||
}) {
|
||||
final nextIndex = _sessions.length + 1;
|
||||
final session = TerminalSession(
|
||||
id: 'session_${DateTime.now().microsecondsSinceEpoch}_$nextIndex',
|
||||
name: name,
|
||||
distroId: distroId,
|
||||
initialOutput: [
|
||||
'Welcome to Terminal ($nextIndex) [$distroId]',
|
||||
'Type "help" for a list of built-in commands or "clear" to clear.',
|
||||
'',
|
||||
],
|
||||
);
|
||||
|
||||
_sessions.add(session);
|
||||
|
||||
if (activate || _sessions.length == 1) {
|
||||
_activeIndex = _sessions.length - 1;
|
||||
}
|
||||
|
||||
notifyListeners();
|
||||
return session;
|
||||
}
|
||||
|
||||
/// 切换当前激活的会话
|
||||
void selectSession(int index) {
|
||||
if (index >= 0 && index < _sessions.length) {
|
||||
if (_activeIndex != index) {
|
||||
_activeIndex = index;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 重命名指定终端会话
|
||||
void renameSession(String id, String newName) {
|
||||
final trimmed = newName.trim();
|
||||
if (trimmed.isEmpty) return;
|
||||
|
||||
final index = _sessions.indexWhere((s) => s.id == id);
|
||||
if (index != -1) {
|
||||
_sessions[index].name = trimmed;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
/// 删除指定终端会话
|
||||
void removeSession(String id) {
|
||||
final index = _sessions.indexWhere((s) => s.id == id);
|
||||
if (index == -1) return;
|
||||
|
||||
final removedSession = _sessions.removeAt(index);
|
||||
removedSession.dispose();
|
||||
|
||||
if (_sessions.isEmpty) {
|
||||
_activeIndex = 0;
|
||||
} else if (_activeIndex >= _sessions.length) {
|
||||
_activeIndex = _sessions.length - 1;
|
||||
} else if (_activeIndex > index) {
|
||||
_activeIndex--;
|
||||
}
|
||||
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// 拖拽重新排序终端会话(接收 onReorderItem 传入的已修正目标索引)
|
||||
void reorderSessions(int oldIndex, int newIndex) {
|
||||
if (oldIndex < 0 || oldIndex >= _sessions.length) return;
|
||||
if (newIndex < 0 || newIndex >= _sessions.length) return;
|
||||
if (oldIndex == newIndex) return;
|
||||
|
||||
final activeSessionId = activeSession?.id;
|
||||
|
||||
final session = _sessions.removeAt(oldIndex);
|
||||
_sessions.insert(newIndex, session);
|
||||
|
||||
if (activeSessionId != null) {
|
||||
final foundIndex = _sessions.indexWhere((s) => s.id == activeSessionId);
|
||||
if (foundIndex != -1) {
|
||||
_activeIndex = foundIndex;
|
||||
}
|
||||
}
|
||||
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// 向指定终端会话追加输出并通知更新
|
||||
void appendOutput(TerminalSession session, String line) {
|
||||
session.appendOutput(line);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// 执行简单交互命令(支持 help, clear, echo, date 等,后续可无缝接入 PTY / Shell)
|
||||
void executeCommand(TerminalSession session, String rawInput) {
|
||||
final input = rawInput.trim();
|
||||
session.appendOutput('\$ $rawInput');
|
||||
|
||||
if (input.isEmpty) {
|
||||
notifyListeners();
|
||||
return;
|
||||
}
|
||||
|
||||
final parts = input.split(RegExp(r'\s+'));
|
||||
final cmd = parts.first.toLowerCase();
|
||||
|
||||
switch (cmd) {
|
||||
case 'clear':
|
||||
session.clear();
|
||||
break;
|
||||
case 'help':
|
||||
session.appendOutput('Built-in terminal commands:');
|
||||
session.appendOutput(' clear Clear the terminal screen');
|
||||
session.appendOutput(' date Show current date and time');
|
||||
session.appendOutput(' echo <msg> Echo text back');
|
||||
session.appendOutput(' help Show this help information');
|
||||
session.appendOutput(' sessions List active sessions');
|
||||
break;
|
||||
case 'date':
|
||||
session.appendOutput(DateTime.now().toString());
|
||||
break;
|
||||
case 'echo':
|
||||
session.appendOutput(parts.skip(1).join(' '));
|
||||
break;
|
||||
case 'sessions':
|
||||
session.appendOutput('Total sessions: ${_sessions.length}');
|
||||
for (int i = 0; i < _sessions.length; i++) {
|
||||
final s = _sessions[i];
|
||||
final marker = (i == _activeIndex) ? '*' : ' ';
|
||||
session.appendOutput(' $marker (${i + 1}) ${s.name} [${s.id}]');
|
||||
}
|
||||
break;
|
||||
default:
|
||||
session.appendOutput('command not found: $cmd');
|
||||
break;
|
||||
}
|
||||
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
for (final session in _sessions) {
|
||||
session.dispose();
|
||||
}
|
||||
_sessions.clear();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user