1
This commit is contained in:
1 parent
b03ece33a2
commit
54c3083c24
29 files changed
+2150
-549
No files matched your search
@@ -1,111 +0,0 @@
|
||||
import 'package:code_editor/models/editor_tab_item.dart';
|
||||
import 'package:code_editor/models/editor_theme.dart';
|
||||
import 'package:code_editor/models/file_directory_history.dart';
|
||||
import 'package:code_editor/models/file_item.dart';
|
||||
import 'package:code_editor/providers/project_provider.dart';
|
||||
import 'package:code_editor/providers/settings_provider.dart';
|
||||
import 'package:code_editor/providers/tab_provider.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// 组合门面 Provider,委托给 SettingsProvider, ProjectProvider, TabProvider,
|
||||
/// 保持向下兼容性与测试透明度。
|
||||
class EditorProvider extends ChangeNotifier {
|
||||
final SettingsProvider settingsProvider;
|
||||
final ProjectProvider projectProvider;
|
||||
final TabProvider tabProvider;
|
||||
|
||||
EditorProvider({
|
||||
SettingsProvider? settings,
|
||||
ProjectProvider? project,
|
||||
TabProvider? tab,
|
||||
}) : settingsProvider = settings ?? SettingsProvider(),
|
||||
projectProvider = project ?? ProjectProvider(),
|
||||
tabProvider = tab ?? TabProvider() {
|
||||
tabProvider.bindProjectProvider(projectProvider);
|
||||
settingsProvider.addListener(notifyListeners);
|
||||
projectProvider.addListener(notifyListeners);
|
||||
tabProvider.addListener(notifyListeners);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
settingsProvider.removeListener(notifyListeners);
|
||||
projectProvider.removeListener(notifyListeners);
|
||||
tabProvider.removeListener(notifyListeners);
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> init() async {
|
||||
await settingsProvider.init();
|
||||
await projectProvider.init();
|
||||
await tabProvider.init();
|
||||
}
|
||||
|
||||
// =================== Settings Provider Getters & Methods ===================
|
||||
EditorTheme get editorTheme => settingsProvider.editorTheme;
|
||||
ThemeMode get appThemeMode => settingsProvider.appThemeMode;
|
||||
double get fontSize => settingsProvider.fontSize;
|
||||
bool get wordWrap => settingsProvider.wordWrap;
|
||||
Locale? get locale => settingsProvider.locale;
|
||||
|
||||
Future<void> setEditorTheme(EditorTheme theme) => settingsProvider.setEditorTheme(theme);
|
||||
Future<void> setAppThemeMode(ThemeMode mode) => settingsProvider.setAppThemeMode(mode);
|
||||
Future<void> setFontSize(double size) => settingsProvider.setFontSize(size);
|
||||
Future<void> setWordWrap(bool wrap) => settingsProvider.setWordWrap(wrap);
|
||||
Future<void> setLocale(Locale? newLocale) => settingsProvider.setLocale(newLocale);
|
||||
|
||||
// =================== Project Provider Getters & Methods ===================
|
||||
FileDirectoryHistory get history => projectProvider.history;
|
||||
String? get rootPath => projectProvider.rootPath;
|
||||
List<String> get openDirectoryPaths => projectProvider.openDirectoryPaths;
|
||||
List<FileItem> get items => projectProvider.items;
|
||||
bool get isLoading => projectProvider.isLoading;
|
||||
FileItem? get cutItem => projectProvider.cutItem;
|
||||
FileItem? get copiedItem => projectProvider.copiedItem;
|
||||
bool get canPaste => projectProvider.canPaste;
|
||||
|
||||
bool isItemCut(String path) => projectProvider.isItemCut(path);
|
||||
Future<void> openDirectory() => projectProvider.openDirectory();
|
||||
Future<void> switchProject(FileDirectoryHistory selectedHistory) => projectProvider.switchProject(selectedHistory);
|
||||
Future<void> toggleDirectory(FileItem item, bool expanded) => projectProvider.toggleDirectory(item, expanded);
|
||||
Future<void> refreshTree() => projectProvider.refreshTree();
|
||||
void cut(FileItem item) => projectProvider.cut(item);
|
||||
void copy(FileItem item) => projectProvider.copy(item);
|
||||
Future<void> paste(FileItem targetDir) => projectProvider.paste(targetDir);
|
||||
Future<void> createFile(String parentDir, String name) => projectProvider.createFile(parentDir, name);
|
||||
Future<void> createDirectory(String parentDir, String name) => projectProvider.createDirectory(parentDir, name);
|
||||
Future<void> rename(FileItem item, String newName) => projectProvider.rename(item, newName);
|
||||
Future<void> delete(FileItem item) => projectProvider.delete(item);
|
||||
|
||||
@visibleForTesting
|
||||
void setHistoryForTesting(FileDirectoryHistory history) {
|
||||
// ignore: invalid_use_of_visible_for_testing_member
|
||||
projectProvider.setHistoryForTesting(history);
|
||||
}
|
||||
|
||||
// =================== Tab Provider Getters & Methods ===================
|
||||
List<EditorTabItem> get openTabs => tabProvider.openTabs;
|
||||
EditorTabItem? get activeTab => tabProvider.activeTab;
|
||||
String? get currentFilePath => tabProvider.currentFilePath;
|
||||
bool get isModified => tabProvider.isModified;
|
||||
|
||||
bool isFileSelected(String path) => tabProvider.isFileSelected(path);
|
||||
Future<void> selectFile(FileItem item) => tabProvider.selectFile(item);
|
||||
Future<void> openFile(String path, {String? content}) => tabProvider.openFile(path, content: content);
|
||||
Future<bool> closeTab(BuildContext context, int index) => tabProvider.closeTab(context, index);
|
||||
Future<void> reorderTabs(int oldIndex, int newIndex) => tabProvider.reorderTabs(oldIndex, newIndex);
|
||||
Future<bool> saveTab(EditorTabItem tab) => tabProvider.saveTab(tab);
|
||||
Future<bool> saveCurrentFile() => tabProvider.saveCurrentFile();
|
||||
Future<bool> saveAllFiles() => tabProvider.saveAllFiles();
|
||||
Future<bool> checkUnsavedChanges(BuildContext context) => tabProvider.checkUnsavedChanges(context);
|
||||
void registerSaveHandler(Future<bool> Function()? handler) => tabProvider.registerSaveHandler(handler);
|
||||
void setModified(bool modified) => tabProvider.setModified(modified);
|
||||
void updateActiveTabContent(String content, {bool? isModified}) =>
|
||||
tabProvider.updateActiveTabContent(content, isModified: isModified);
|
||||
|
||||
@visibleForTesting
|
||||
void setOpenTabsForTesting(List<EditorTabItem> tabs, {String? activePath}) {
|
||||
// ignore: invalid_use_of_visible_for_testing_member
|
||||
tabProvider.setOpenTabsForTesting(tabs, activePath: activePath);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import 'dart:io';
|
||||
import 'package:code_editor/models/file_directory_history.dart';
|
||||
import 'package:code_editor/models/project_history.dart';
|
||||
import 'package:code_editor/models/file_item.dart';
|
||||
import 'package:code_editor/services/file_directory_history_service.dart';
|
||||
import 'package:code_editor/services/project_history_service.dart';
|
||||
import 'package:code_editor/services/file_service.dart';
|
||||
import 'package:code_editor/services/file_watcher_service.dart';
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
@@ -9,15 +9,22 @@ import 'package:flutter/material.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
|
||||
class ProjectProvider extends ChangeNotifier {
|
||||
FileDirectoryHistory _history = const FileDirectoryHistory(
|
||||
//项目历史
|
||||
ProjectHistory _history = const ProjectHistory(
|
||||
rootPath: null,
|
||||
lastOpenedFilePath: null,
|
||||
);
|
||||
|
||||
//根目录的一级子文件目录
|
||||
List<FileItem> _items = [];
|
||||
bool _isLoading = false;
|
||||
|
||||
//当前剪切复制的文件
|
||||
FileItem? _cutItem;
|
||||
FileItem? _copiedItem;
|
||||
|
||||
//是否正在加载项目
|
||||
bool _isLoading = false;
|
||||
|
||||
// 跨 Provider 协同回调
|
||||
void Function(String oldPath, String newPath)? onEntityRenamed;
|
||||
void Function(String deletedPath)? onEntityDeleted;
|
||||
@@ -25,7 +32,7 @@ class ProjectProvider extends ChangeNotifier {
|
||||
void Function(String modifiedPath)? onExternalFileModified;
|
||||
void Function(String deletedPath)? onExternalFileDeleted;
|
||||
|
||||
FileDirectoryHistory get history => _history;
|
||||
ProjectHistory get history => _history;
|
||||
String? get rootPath => _history.rootPath;
|
||||
List<String> get openDirectoryPaths => _history.openDirectoryPaths;
|
||||
List<FileItem> get items => _items;
|
||||
@@ -44,7 +51,7 @@ class ProjectProvider extends ChangeNotifier {
|
||||
}
|
||||
|
||||
@visibleForTesting
|
||||
void setHistoryForTesting(FileDirectoryHistory history) {
|
||||
void setHistoryForTesting(ProjectHistory history) {
|
||||
_history = history;
|
||||
notifyListeners();
|
||||
}
|
||||
@@ -55,13 +62,14 @@ class ProjectProvider extends ChangeNotifier {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
//从历史加载项目
|
||||
Future<void> _loadProjectFromHistory() async {
|
||||
_isLoading = true;
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
final lastHistory = await FileDirectoryHistoryService.instance.getLastHistory();
|
||||
if (lastHistory == null) {
|
||||
final lastHistory = await ProjectHistoryService.instance.getLastHistory();
|
||||
if(lastHistory == null) {
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
return;
|
||||
@@ -84,7 +92,7 @@ class ProjectProvider extends ChangeNotifier {
|
||||
: [];
|
||||
|
||||
_items = items;
|
||||
_history = FileDirectoryHistory(
|
||||
_history = ProjectHistory(
|
||||
rootPath: cleanPath,
|
||||
lastOpenedFilePath: lastHistory.lastOpenedFilePath,
|
||||
openDirectoryPaths: validOpenPaths,
|
||||
@@ -96,18 +104,19 @@ class ProjectProvider extends ChangeNotifier {
|
||||
|
||||
// 通知外部(例如 TabProvider)同步标签页历史
|
||||
onProjectChanged?.call(cleanPath, lastHistory.openFilePaths, lastHistory.lastOpenedFilePath);
|
||||
} catch (e) {
|
||||
}catch(e) {
|
||||
debugPrint('加载项目历史失败: $e');
|
||||
} finally {
|
||||
}finally {
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
//打开新的项目
|
||||
Future<void> openDirectory() async {
|
||||
try {
|
||||
final String? selectedPath = await FilePicker.platform.getDirectoryPath();
|
||||
if (selectedPath == null || selectedPath.trim().isEmpty) {
|
||||
if(selectedPath == null || selectedPath.trim().isEmpty) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -115,7 +124,7 @@ class ProjectProvider extends ChangeNotifier {
|
||||
final currentRoot = _history.rootPath != null ? p.normalize(_history.rootPath!) : null;
|
||||
final isSameRoot = currentRoot != null && cleanPath == currentRoot;
|
||||
|
||||
await FileDirectoryHistoryService.instance.recordHistory(
|
||||
await ProjectHistoryService.instance.recordHistory(
|
||||
rootPath: cleanPath,
|
||||
lastOpenedFilePath: isSameRoot ? _history.lastOpenedFilePath : null,
|
||||
openDirectoryPaths: isSameRoot ? _history.openDirectoryPaths : const [],
|
||||
@@ -126,16 +135,17 @@ class ProjectProvider extends ChangeNotifier {
|
||||
_copiedItem = null;
|
||||
|
||||
await _loadProjectFromHistory();
|
||||
} catch (e) {
|
||||
}catch (e) {
|
||||
debugPrint('打开文件夹失败: $e');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> switchProject(FileDirectoryHistory selectedHistory) async {
|
||||
//选择项目历史
|
||||
Future<void> switchProject(ProjectHistory selectedHistory) async {
|
||||
final selectedRoot = selectedHistory.rootPath;
|
||||
if (selectedRoot == null || selectedRoot.trim().isEmpty) return;
|
||||
if(selectedRoot == null || selectedRoot.trim().isEmpty) return;
|
||||
|
||||
await FileDirectoryHistoryService.instance.recordHistory(
|
||||
await ProjectHistoryService.instance.recordHistory(
|
||||
rootPath: selectedRoot,
|
||||
lastOpenedFilePath: selectedHistory.lastOpenedFilePath,
|
||||
openDirectoryPaths: selectedHistory.openDirectoryPaths,
|
||||
@@ -148,16 +158,31 @@ class ProjectProvider extends ChangeNotifier {
|
||||
await _loadProjectFromHistory();
|
||||
}
|
||||
|
||||
/// 关闭当前项目
|
||||
Future<void> closeProject() async {
|
||||
_startWatcher(null);
|
||||
_cutItem = null;
|
||||
_copiedItem = null;
|
||||
_items = [];
|
||||
_history = const ProjectHistory(
|
||||
rootPath: null,
|
||||
lastOpenedFilePath: null,
|
||||
);
|
||||
onProjectChanged?.call(null, const [], null);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
//展开文件夹
|
||||
Future<void> toggleDirectory(FileItem item, bool expanded) async {
|
||||
final cleanPath = _history.rootPath;
|
||||
if (cleanPath == null) return;
|
||||
if(cleanPath == null) return;
|
||||
|
||||
final updatedOpenPaths = List<String>.from(_history.openDirectoryPaths);
|
||||
if (expanded) {
|
||||
if (!updatedOpenPaths.contains(item.path)) {
|
||||
if(expanded) {
|
||||
if(!updatedOpenPaths.contains(item.path)) {
|
||||
updatedOpenPaths.add(item.path);
|
||||
}
|
||||
if (item.children.isEmpty) {
|
||||
if(item.children.isEmpty) {
|
||||
final children = await FileService.instance.buildTree(
|
||||
item.path,
|
||||
depth: item.depth + 1,
|
||||
@@ -166,17 +191,17 @@ class ProjectProvider extends ChangeNotifier {
|
||||
item.children.clear();
|
||||
item.children.addAll(children);
|
||||
}
|
||||
} else {
|
||||
}else {
|
||||
updatedOpenPaths.remove(item.path);
|
||||
}
|
||||
|
||||
await FileDirectoryHistoryService.instance.recordHistory(
|
||||
await ProjectHistoryService.instance.recordHistory(
|
||||
rootPath: cleanPath,
|
||||
lastOpenedFilePath: _history.lastOpenedFilePath,
|
||||
openDirectoryPaths: updatedOpenPaths,
|
||||
openFilePaths: _history.openFilePaths,
|
||||
);
|
||||
_history = FileDirectoryHistory(
|
||||
_history = ProjectHistory(
|
||||
rootPath: cleanPath,
|
||||
lastOpenedFilePath: _history.lastOpenedFilePath,
|
||||
openDirectoryPaths: updatedOpenPaths,
|
||||
@@ -187,7 +212,7 @@ class ProjectProvider extends ChangeNotifier {
|
||||
|
||||
Future<void> refreshTree() async {
|
||||
final currentRoot = _history.rootPath;
|
||||
if (currentRoot == null || currentRoot.trim().isEmpty) return;
|
||||
if(currentRoot == null || currentRoot.trim().isEmpty) return;
|
||||
|
||||
try {
|
||||
final items = await FileService.instance.buildTree(
|
||||
@@ -196,11 +221,11 @@ class ProjectProvider extends ChangeNotifier {
|
||||
);
|
||||
|
||||
String? currentFile = _history.lastOpenedFilePath;
|
||||
if (currentFile != null) {
|
||||
if(currentFile != null) {
|
||||
final exists = await FileService.instance.entityExists(currentFile);
|
||||
if (!exists) {
|
||||
if(!exists) {
|
||||
currentFile = null;
|
||||
await FileDirectoryHistoryService.instance.recordHistory(
|
||||
await ProjectHistoryService.instance.recordHistory(
|
||||
rootPath: currentRoot,
|
||||
lastOpenedFilePath: null,
|
||||
openDirectoryPaths: _history.openDirectoryPaths,
|
||||
@@ -210,8 +235,8 @@ class ProjectProvider extends ChangeNotifier {
|
||||
}
|
||||
|
||||
_items = items;
|
||||
if (currentFile != _history.lastOpenedFilePath) {
|
||||
_history = FileDirectoryHistory(
|
||||
if(currentFile != _history.lastOpenedFilePath) {
|
||||
_history = ProjectHistory(
|
||||
rootPath: currentRoot,
|
||||
lastOpenedFilePath: currentFile,
|
||||
openDirectoryPaths: _history.openDirectoryPaths,
|
||||
@@ -219,7 +244,7 @@ class ProjectProvider extends ChangeNotifier {
|
||||
);
|
||||
}
|
||||
notifyListeners();
|
||||
} catch (e) {
|
||||
}catch (e) {
|
||||
debugPrint('刷新目录树失败: $e');
|
||||
}
|
||||
}
|
||||
@@ -237,14 +262,14 @@ class ProjectProvider extends ChangeNotifier {
|
||||
}
|
||||
|
||||
Future<void> paste(FileItem targetDir) async {
|
||||
if (!targetDir.isDirectory) return;
|
||||
if(!targetDir.isDirectory) return;
|
||||
|
||||
if (_cutItem != null) {
|
||||
if(_cutItem != null) {
|
||||
final source = _cutItem!;
|
||||
await FileService.instance.moveEntity(source.path, targetDir.path);
|
||||
_cutItem = null;
|
||||
await refreshTree();
|
||||
} else if (_copiedItem != null) {
|
||||
}else if (_copiedItem != null) {
|
||||
final source = _copiedItem!;
|
||||
await FileService.instance.copyEntity(source.path, targetDir.path);
|
||||
await refreshTree();
|
||||
@@ -283,7 +308,7 @@ class ProjectProvider extends ChangeNotifier {
|
||||
}
|
||||
|
||||
void _startWatcher(String? rootPath) {
|
||||
if (rootPath == null || rootPath.isEmpty) {
|
||||
if(rootPath == null || rootPath.isEmpty) {
|
||||
FileWatcherService.instance.stopWatching();
|
||||
return;
|
||||
}
|
||||
@@ -296,15 +321,15 @@ class ProjectProvider extends ChangeNotifier {
|
||||
void _onFileSystemEvents(List<FileSystemEvent> events) {
|
||||
bool needRefreshTree = false;
|
||||
|
||||
for (final event in events) {
|
||||
for(final event in events) {
|
||||
final normalizedPath = p.normalize(event.path);
|
||||
|
||||
if (event is FileSystemCreateEvent) {
|
||||
if(event is FileSystemCreateEvent) {
|
||||
needRefreshTree = true;
|
||||
} else if (event is FileSystemDeleteEvent) {
|
||||
}else if (event is FileSystemDeleteEvent) {
|
||||
needRefreshTree = true;
|
||||
onExternalFileDeleted?.call(normalizedPath);
|
||||
} else if (event is FileSystemMoveEvent) {
|
||||
}else if (event is FileSystemMoveEvent) {
|
||||
needRefreshTree = true;
|
||||
final dest = event.destination;
|
||||
if (dest != null) {
|
||||
@@ -312,7 +337,7 @@ class ProjectProvider extends ChangeNotifier {
|
||||
} else {
|
||||
onExternalFileDeleted?.call(normalizedPath);
|
||||
}
|
||||
} else if (event is FileSystemModifyEvent) {
|
||||
}else if (event is FileSystemModifyEvent) {
|
||||
if (event.isDirectory) {
|
||||
needRefreshTree = true;
|
||||
} else {
|
||||
@@ -321,7 +346,7 @@ class ProjectProvider extends ChangeNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
if (needRefreshTree) {
|
||||
if(needRefreshTree) {
|
||||
refreshTree();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,24 +1,40 @@
|
||||
import 'dart:convert';
|
||||
import 'package:code_editor/models/editor_theme.dart';
|
||||
import 'package:code_editor/models/virtual_keyboard_config.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class SettingsProvider extends ChangeNotifier {
|
||||
static const String _keyEditorTheme = 'editor_theme_id';
|
||||
static const String _keyAppThemeMode = 'app_theme_mode';
|
||||
static const String _keyEditorFontSize = 'editor_font_size';
|
||||
static const String _keyWordWrap = 'editor_word_wrap';
|
||||
//外观
|
||||
static const String _keyAppThemeMode = 'app_theme_mode'; //应用主题
|
||||
|
||||
//编辑区
|
||||
static const String _keyEditorTheme = 'editor_theme_id'; //代码主题
|
||||
static const String _keyEditorFontSize = 'editor_font_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 _keyAppLocale = 'app_locale';
|
||||
|
||||
EditorTheme _editorTheme = EditorTheme.atomOneDark;
|
||||
ThemeMode _appThemeMode = ThemeMode.system;
|
||||
EditorTheme _editorTheme = EditorTheme.atomOneDark;
|
||||
double _fontSize = 14.0;
|
||||
bool _wordWrap = true;
|
||||
bool _enableVirtualKeyboard = defaultTargetPlatform == TargetPlatform.android || defaultTargetPlatform == TargetPlatform.iOS;
|
||||
String _virtualKeyboardConfigJson = VirtualKeyboardConfig.defaultJsonPretty();
|
||||
VirtualKeyboardConfig _virtualKeyboardConfig = VirtualKeyboardConfig.defaultConfiguration();
|
||||
Locale? _locale;
|
||||
|
||||
EditorTheme get editorTheme => _editorTheme;
|
||||
ThemeMode get appThemeMode => _appThemeMode;
|
||||
EditorTheme get editorTheme => _editorTheme;
|
||||
double get fontSize => _fontSize;
|
||||
bool get wordWrap => _wordWrap;
|
||||
bool get enableVirtualKeyboard => _enableVirtualKeyboard;
|
||||
String get virtualKeyboardConfigJson => _virtualKeyboardConfigJson;
|
||||
VirtualKeyboardConfig get virtualKeyboardConfig => _virtualKeyboardConfig;
|
||||
Locale? get locale => _locale;
|
||||
|
||||
Future<void> init() async {
|
||||
@@ -28,90 +44,150 @@ class SettingsProvider extends ChangeNotifier {
|
||||
Future<void> _loadSettings() async {
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final themeId = prefs.getString(_keyEditorTheme);
|
||||
_editorTheme = EditorTheme.fromId(themeId);
|
||||
|
||||
//设置外观
|
||||
final themeModeName = prefs.getString(_keyAppThemeMode);
|
||||
if (themeModeName != null) {
|
||||
if(themeModeName != null) {
|
||||
_appThemeMode = ThemeMode.values.firstWhere(
|
||||
(e) => e.name == themeModeName,
|
||||
orElse: () => ThemeMode.system,
|
||||
);
|
||||
}
|
||||
|
||||
//设置编辑区
|
||||
final themeId = prefs.getString(_keyEditorTheme);
|
||||
_editorTheme = EditorTheme.fromId(themeId);
|
||||
|
||||
|
||||
final savedFontSize = prefs.getDouble(_keyEditorFontSize);
|
||||
if (savedFontSize != null && savedFontSize >= 10 && savedFontSize <= 32) {
|
||||
if(savedFontSize != null && savedFontSize >= 10 && savedFontSize <= 32) {
|
||||
_fontSize = savedFontSize;
|
||||
}
|
||||
|
||||
final savedWordWrap = prefs.getBool(_keyWordWrap);
|
||||
if (savedWordWrap != null) {
|
||||
if(savedWordWrap != null) {
|
||||
_wordWrap = savedWordWrap;
|
||||
}
|
||||
|
||||
final savedEnableVirtualKeyboard = prefs.getBool(_keyEnableVirtualKeyboard);
|
||||
if (savedEnableVirtualKeyboard != null) {
|
||||
_enableVirtualKeyboard = savedEnableVirtualKeyboard;
|
||||
}
|
||||
|
||||
final savedKeyboardConfig = prefs.getString(_keyVirtualKeyboardConfig);
|
||||
if (savedKeyboardConfig != null && savedKeyboardConfig.trim().isNotEmpty) {
|
||||
final err = VirtualKeyboardConfig.validateJson(savedKeyboardConfig);
|
||||
if (err == null) {
|
||||
try {
|
||||
final decoded = jsonDecode(savedKeyboardConfig) as Map<String, dynamic>;
|
||||
_virtualKeyboardConfig = VirtualKeyboardConfig.fromJson(decoded);
|
||||
_virtualKeyboardConfigJson = savedKeyboardConfig;
|
||||
} catch (_) {}
|
||||
}
|
||||
}
|
||||
|
||||
//设置语言
|
||||
final savedLocale = prefs.getString(_keyAppLocale);
|
||||
if (savedLocale != null && savedLocale.isNotEmpty) {
|
||||
if(savedLocale != null && savedLocale.isNotEmpty) {
|
||||
_locale = Locale(savedLocale);
|
||||
} else {
|
||||
_locale = null; // null 表示跟随系统
|
||||
_locale = null;
|
||||
}
|
||||
|
||||
notifyListeners();
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
Future<void> setEditorTheme(EditorTheme theme) async {
|
||||
if (_editorTheme.id == theme.id) return;
|
||||
_editorTheme = theme;
|
||||
notifyListeners();
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setString(_keyEditorTheme, theme.id);
|
||||
} catch (_) {}
|
||||
}catch (_) {}
|
||||
}
|
||||
|
||||
Future<void> setAppThemeMode(ThemeMode mode) async {
|
||||
if (_appThemeMode == mode) return;
|
||||
if(_appThemeMode == mode) return;
|
||||
_appThemeMode = mode;
|
||||
notifyListeners();
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setString(_keyAppThemeMode, mode.name);
|
||||
} catch (_) {}
|
||||
}catch (_) {}
|
||||
}
|
||||
|
||||
Future<void> setEditorTheme(EditorTheme theme) async {
|
||||
if(_editorTheme.id == theme.id) return;
|
||||
_editorTheme = theme;
|
||||
notifyListeners();
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setString(_keyEditorTheme, theme.id);
|
||||
}catch(_) {}
|
||||
}
|
||||
|
||||
Future<void> setFontSize(double size) async {
|
||||
final clamped = size.clamp(10.0, 30.0);
|
||||
if (_fontSize == clamped) return;
|
||||
if(_fontSize == clamped) return;
|
||||
_fontSize = clamped;
|
||||
notifyListeners();
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setDouble(_keyEditorFontSize, clamped);
|
||||
} catch (_) {}
|
||||
}catch (_) {}
|
||||
}
|
||||
|
||||
Future<void> setWordWrap(bool wrap) async {
|
||||
if (_wordWrap == wrap) return;
|
||||
if(_wordWrap == wrap) return;
|
||||
_wordWrap = wrap;
|
||||
notifyListeners();
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setBool(_keyWordWrap, wrap);
|
||||
}catch (_) {}
|
||||
}
|
||||
|
||||
Future<void> setEnableVirtualKeyboard(bool enable) async {
|
||||
if (_enableVirtualKeyboard == enable) return;
|
||||
_enableVirtualKeyboard = enable;
|
||||
notifyListeners();
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setBool(_keyEnableVirtualKeyboard, enable);
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
Future<bool> setVirtualKeyboardConfig(String jsonStr) async {
|
||||
final err = VirtualKeyboardConfig.validateJson(jsonStr);
|
||||
if (err != null) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
final decoded = jsonDecode(jsonStr) as Map<String, dynamic>;
|
||||
_virtualKeyboardConfig = VirtualKeyboardConfig.fromJson(decoded);
|
||||
_virtualKeyboardConfigJson = jsonStr;
|
||||
notifyListeners();
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setString(_keyVirtualKeyboardConfig, jsonStr);
|
||||
return true;
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> resetVirtualKeyboardConfig() async {
|
||||
_virtualKeyboardConfig = VirtualKeyboardConfig.defaultConfiguration();
|
||||
_virtualKeyboardConfigJson = VirtualKeyboardConfig.defaultJsonPretty();
|
||||
notifyListeners();
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.remove(_keyVirtualKeyboardConfig);
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
Future<void> setLocale(Locale? newLocale) async {
|
||||
if (_locale == newLocale) return;
|
||||
if(_locale == newLocale) return;
|
||||
_locale = newLocale;
|
||||
notifyListeners();
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
if (newLocale == null) {
|
||||
if(newLocale == null) {
|
||||
await prefs.remove(_keyAppLocale);
|
||||
} else {
|
||||
await prefs.setString(_keyAppLocale, newLocale.languageCode);
|
||||
}
|
||||
} catch (_) {}
|
||||
}catch (_) {}
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@ import 'package:code_editor/l10n/app_localizations.dart';
|
||||
import 'package:code_editor/models/editor_tab_item.dart';
|
||||
import 'package:code_editor/models/file_item.dart';
|
||||
import 'package:code_editor/providers/project_provider.dart';
|
||||
import 'package:code_editor/services/file_directory_history_service.dart';
|
||||
import 'package:code_editor/services/project_history_service.dart';
|
||||
import 'package:code_editor/services/file_service.dart';
|
||||
import 'package:code_editor/services/file_watcher_service.dart';
|
||||
import 'package:code_editor/utils/dialog_utils.dart';
|
||||
@@ -48,7 +48,7 @@ class TabProvider extends ChangeNotifier {
|
||||
|
||||
Future<void> _loadTabsFromHistory() async {
|
||||
try {
|
||||
final lastHistory = await FileDirectoryHistoryService.instance.getLastHistory();
|
||||
final lastHistory = await ProjectHistoryService.instance.getLastHistory();
|
||||
if (lastHistory == null) return;
|
||||
await handleProjectChanged(
|
||||
lastHistory.rootPath,
|
||||
@@ -237,7 +237,7 @@ class TabProvider extends ChangeNotifier {
|
||||
final root = _projectProvider?.rootPath;
|
||||
if (root != null) {
|
||||
final openPaths = _openTabs.map((t) => t.path).toList();
|
||||
await FileDirectoryHistoryService.instance.recordHistory(
|
||||
await ProjectHistoryService.instance.recordHistory(
|
||||
rootPath: root,
|
||||
lastOpenedFilePath: _activeFilePath,
|
||||
openDirectoryPaths: _projectProvider?.openDirectoryPaths ?? const [],
|
||||
@@ -348,7 +348,6 @@ class TabProvider extends ChangeNotifier {
|
||||
if (!saved) return false;
|
||||
break;
|
||||
case SavePromptResult.discard:
|
||||
tab.isModified = false;
|
||||
break;
|
||||
case SavePromptResult.cancel:
|
||||
return false;
|
||||
@@ -374,6 +373,20 @@ class TabProvider extends ChangeNotifier {
|
||||
return true;
|
||||
}
|
||||
|
||||
/// 关闭所有打开的标签页
|
||||
Future<bool> closeAllTabs(BuildContext context) async {
|
||||
if (_openTabs.isEmpty) return true;
|
||||
final canProceed = await checkUnsavedChanges(context);
|
||||
if (!canProceed) return false;
|
||||
|
||||
_openTabs.clear();
|
||||
_activeFilePath = null;
|
||||
_isModified = false;
|
||||
await _persistTabsHistory();
|
||||
notifyListeners();
|
||||
return true;
|
||||
}
|
||||
|
||||
Future<void> reorderTabs(int oldIndex, int newIndex) async {
|
||||
if (oldIndex < 0 || oldIndex >= _openTabs.length) return;
|
||||
if (newIndex < 0 || newIndex >= _openTabs.length) return;
|
||||
@@ -456,11 +469,6 @@ class TabProvider extends ChangeNotifier {
|
||||
return saved;
|
||||
|
||||
case SavePromptResult.discard:
|
||||
for (final tab in _openTabs) {
|
||||
tab.isModified = false;
|
||||
}
|
||||
_isModified = false;
|
||||
notifyListeners();
|
||||
return true;
|
||||
|
||||
case SavePromptResult.cancel:
|
||||
|
||||
Reference in new issue
Block a user