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.
79 lines
2.1 KiB
79 lines
2.1 KiB
import 'package:flutter/material.dart'; |
|
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'; |
|
|
|
class TabManager with ChangeNotifier { |
|
final List<AppTab> _tabs = []; |
|
String? _activeTabId; |
|
|
|
List<AppTab> get tabs => _tabs; |
|
String? get activeTabId => _activeTabId; |
|
|
|
BaseViewState? _activeViewState; |
|
|
|
AppTab? get activeTab { |
|
if (_activeTabId == null) return null; |
|
try { |
|
return _tabs.firstWhere((tab) => tab.id == _activeTabId); |
|
} catch (e) { |
|
Logger().error("找不到活动选项卡: $_activeTabId", source: 'EditorProvider'); |
|
return null; |
|
} |
|
} |
|
|
|
Future<void> addTab( |
|
String id, { |
|
String title = '未命名', |
|
String? type, |
|
IconData? icon, |
|
String content = '', |
|
}) async { |
|
final newTab = AppTab(id: id, title: title, type: type, icon: icon, content: content); |
|
|
|
_tabs.add(newTab); |
|
_activeTabId = newTab.id; |
|
notifyListeners(); |
|
} |
|
|
|
AppTab? getTabById(String tabId) { |
|
try { |
|
return _tabs.firstWhere((tab) => tab.id == tabId); |
|
} catch (e) { |
|
Logger().error("找不到选项卡: ${tabId}", source: 'EditorProvider'); |
|
return null; |
|
} |
|
} |
|
|
|
void closeTab(String tabId) { |
|
Logger().info('关闭选项卡: $tabId'); |
|
_tabs.removeWhere((tab) => tab.id == tabId); |
|
if (_activeTabId == tabId) { |
|
_activeTabId = _tabs.isNotEmpty ? _tabs.last.id : null; |
|
} |
|
notifyListeners(); |
|
} |
|
|
|
void setActiveTab(String tabId) { |
|
if (_activeTabId == tabId) return; // 如果已经是活动Tab则不通知 |
|
|
|
Logger().info('设置活动选项卡: $tabId'); |
|
_activeTabId = tabId; |
|
notifyListeners(); |
|
} |
|
|
|
void handleFolderDoubleTap(String folderPath) { |
|
_activeViewState?.onOpenFolder(folderPath); |
|
} |
|
|
|
void handleFileDoubleTap(String filePath) { |
|
_activeViewState?.onOpenFile(filePath); |
|
} |
|
|
|
void setActiveViewState(BaseViewState? state) { |
|
if (_activeViewState != state) { |
|
_activeViewState = state; |
|
// 不需要 notifyListeners(),因为这只是内部状态更新 |
|
} |
|
} |
|
}
|
|
|