You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
2.1 KiB
80 lines
2.1 KiB
2 months ago
|
import 'package:flutter/material.dart';
|
||
2 months ago
|
import 'package:win_text_editor/frame/models/tab_model.dart';
|
||
|
import 'package:win_text_editor/shared/base/base_view.dart';
|
||
|
import 'package:win_text_editor/frame/providers/logger.dart';
|
||
2 months ago
|
|
||
2 months ago
|
class TabManager with ChangeNotifier {
|
||
|
final List<AppTab> _tabs = [];
|
||
2 months ago
|
String? _activeTabId;
|
||
2 months ago
|
|
||
2 months ago
|
List<AppTab> get tabs => _tabs;
|
||
2 months ago
|
String? get activeTabId => _activeTabId;
|
||
2 months ago
|
|
||
2 months ago
|
BaseViewState? _activeViewState;
|
||
2 months ago
|
|
||
2 months ago
|
AppTab? get activeTab {
|
||
2 months ago
|
if (_activeTabId == null) return null;
|
||
|
try {
|
||
|
return _tabs.firstWhere((tab) => tab.id == _activeTabId);
|
||
|
} catch (e) {
|
||
|
Logger().error("找不到活动选项卡: $_activeTabId", source: 'EditorProvider');
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
2 months ago
|
Future<void> addTab(
|
||
|
String id, {
|
||
2 months ago
|
String title = '未命名',
|
||
|
String? type,
|
||
|
IconData? icon,
|
||
|
String content = '',
|
||
|
}) async {
|
||
2 months ago
|
final newTab = AppTab(id: id, title: title, type: type, icon: icon, content: content);
|
||
2 months ago
|
|
||
|
_tabs.add(newTab);
|
||
|
_activeTabId = newTab.id;
|
||
2 months ago
|
notifyListeners();
|
||
2 months ago
|
}
|
||
|
|
||
2 months ago
|
AppTab? getTabById(String tabId) {
|
||
2 months ago
|
try {
|
||
|
return _tabs.firstWhere((tab) => tab.id == tabId);
|
||
|
} catch (e) {
|
||
|
Logger().error("找不到选项卡: ${tabId}", source: 'EditorProvider');
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
2 months ago
|
void closeTab(String tabId) {
|
||
2 months ago
|
Logger().info('关闭选项卡: $tabId');
|
||
2 months ago
|
_tabs.removeWhere((tab) => tab.id == tabId);
|
||
|
if (_activeTabId == tabId) {
|
||
|
_activeTabId = _tabs.isNotEmpty ? _tabs.last.id : null;
|
||
2 months ago
|
}
|
||
2 months ago
|
notifyListeners();
|
||
2 months ago
|
}
|
||
|
|
||
2 months ago
|
void setActiveTab(String tabId) {
|
||
2 months ago
|
if (_activeTabId == tabId) return; // 如果已经是活动Tab则不通知
|
||
|
|
||
2 months ago
|
Logger().info('设置活动选项卡: $tabId');
|
||
2 months ago
|
_activeTabId = tabId;
|
||
|
notifyListeners();
|
||
2 months ago
|
}
|
||
|
|
||
2 months ago
|
void handleFolderDoubleTap(String folderPath) {
|
||
|
_activeViewState?.onOpenFolder(folderPath);
|
||
2 months ago
|
}
|
||
2 months ago
|
|
||
2 months ago
|
void handleFileDoubleTap(String filePath) {
|
||
|
_activeViewState?.onOpenFile(filePath);
|
||
|
}
|
||
2 months ago
|
|
||
2 months ago
|
void setActiveViewState(BaseViewState? state) {
|
||
|
if (_activeViewState != state) {
|
||
|
_activeViewState = state;
|
||
|
// 不需要 notifyListeners(),因为这只是内部状态更新
|
||
2 months ago
|
}
|
||
|
}
|
||
2 months ago
|
}
|