88import com .laker .postman .common .component .combobox .EnvironmentComboBox ;
99import com .laker .postman .common .component .combobox .WorkspaceComboBox ;
1010import com .laker .postman .ioc .BeanFactory ;
11+ import com .laker .postman .model .GitOperation ;
12+ import com .laker .postman .model .RemoteStatus ;
1113import com .laker .postman .model .Workspace ;
14+ import com .laker .postman .model .WorkspaceType ;
1215import com .laker .postman .panel .collections .left .RequestCollectionsLeftPanel ;
1316import com .laker .postman .panel .env .EnvironmentPanel ;
1417import com .laker .postman .panel .topmenu .help .ChangelogDialog ;
1518import com .laker .postman .panel .topmenu .setting .ModernSettingsDialog ;
19+ import com .laker .postman .panel .workspace .components .GitOperationDialog ;
1620import com .laker .postman .service .ExitService ;
1721import com .laker .postman .service .UpdateService ;
1822import com .laker .postman .service .WorkspaceService ;
2327import javax .swing .*;
2428import javax .swing .border .Border ;
2529import java .awt .*;
30+ import java .awt .event .ActionListener ;
2631import java .awt .event .KeyEvent ;
32+ import java .awt .event .MouseAdapter ;
33+ import java .awt .event .MouseEvent ;
2734import java .io .File ;
2835import java .io .IOException ;
2936
@@ -57,11 +64,14 @@ protected void registerListeners() {
5764 }
5865
5966 /**
60- * 重新加载快捷键(快捷键设置修改后调用)
67+ * 重新加载菜单栏(包括菜单项、快捷键、Git 工具栏等所有组件)
68+ * 在以下场景调用:
69+ * 1. 快捷键设置修改后
70+ * 2. 工作区切换后(需要更新 Git 工具栏显示状态)
6171 */
62- public void reloadShortcuts () {
72+ public void reloadMenuBar () {
6373 removeAll ();
64- // 重新创建菜单栏
74+ // 重新创建菜单栏所有组件
6575 initComponents ();
6676 // 刷新界面
6777 revalidate ();
@@ -80,7 +90,9 @@ private void initComponents() {
8090 addSettingMenu ();
8191 addHelpMenu ();
8292 addAboutMenu ();
83- add (Box .createGlue ());
93+
94+ add (Box .createGlue ()); // 添加弹性空间,将后续组件推到右侧
95+
8496 addRightLableAndComboBox ();
8597 }
8698
@@ -232,14 +244,8 @@ private void addAboutMenu() {
232244 add (aboutMenu );
233245 }
234246
235- private void addRightLableAndComboBox () {
236- // 初始化或重新加载环境下拉框
237- if (environmentComboBox == null ) {
238- environmentComboBox = ComboBoxStyleHelper .createWithPanelStyle (EnvironmentComboBox ::new );
239- } else {
240- environmentComboBox .reload ();
241- }
242247
248+ private void addRightLableAndComboBox () {
243249 // 初始化或重新加载工作区下拉框
244250 if (workspaceComboBox == null ) {
245251 workspaceComboBox = ComboBoxStyleHelper .createWithPanelStyle (WorkspaceComboBox ::new );
@@ -248,6 +254,16 @@ private void addRightLableAndComboBox() {
248254 workspaceComboBox .reload ();
249255 }
250256
257+ // 初始化或重新加载环境下拉框
258+ if (environmentComboBox == null ) {
259+ environmentComboBox = ComboBoxStyleHelper .createWithPanelStyle (EnvironmentComboBox ::new );
260+ } else {
261+ environmentComboBox .reload ();
262+ }
263+
264+ // 添加 Git 工具栏(在工作区下拉框左侧)
265+ addGitToolbarIfNeeded ();
266+
251267 // 添加工作区图标和下拉框
252268 JLabel workspaceIconLabel = new JLabel (new FlatSVGIcon ("icons/workspace.svg" , 20 , 20 ));
253269 add (workspaceIconLabel );
@@ -260,7 +276,25 @@ private void addRightLableAndComboBox() {
260276 JLabel envIconLabel = new JLabel (new FlatSVGIcon ("icons/environments.svg" , 20 , 20 ));
261277 add (envIconLabel );
262278 add (environmentComboBox );
279+ }
263280
281+ /**
282+ * 添加 Git 工具栏(仅在当前工作区为 Git 类型时添加)
283+ */
284+ private void addGitToolbarIfNeeded () {
285+ try {
286+ WorkspaceService workspaceService = WorkspaceService .getInstance ();
287+ Workspace currentWorkspace = workspaceService .getCurrentWorkspace ();
288+
289+ // 只有当前工作区是 Git 工作区时才显示 Git 工具栏
290+ if (currentWorkspace != null && currentWorkspace .getType () == WorkspaceType .GIT ) {
291+ JPanel gitToolbarPanel = createGitToolbar (currentWorkspace );
292+ add (gitToolbarPanel );
293+ add (Box .createHorizontalStrut (20 )); // Git 工具栏和工作区图标之间的间距
294+ }
295+ } catch (Exception e ) {
296+ log .error ("Failed to create Git toolbar" , e );
297+ }
264298 }
265299
266300 /**
@@ -274,6 +308,9 @@ private void updateWorkspaceComboBox() {
274308
275309 /**
276310 * 切换到指定工作区
311+ * 包括切换环境变量文件、请求集合文件,并刷新 Git 工具栏
312+ *
313+ * @param workspace 目标工作区
277314 */
278315 private void switchToWorkspace (Workspace workspace ) {
279316 try {
@@ -295,11 +332,12 @@ private void switchToWorkspace(Workspace workspace) {
295332 SingletonFactory .getInstance (RequestCollectionsLeftPanel .class )
296333 .switchWorkspaceAndRefreshUI (SystemUtil .getCollectionPathForWorkspace (workspace ));
297334
298- // 只记录日志,不显示通知弹窗
335+ // 重新加载菜单栏(根据新工作区类型更新 Git 工具栏显示状态)
336+ reloadMenuBar ();
337+
299338 log .info ("Switched to workspace: {}" , workspace .getName ());
300339 } catch (Exception e ) {
301340 log .error ("Failed to switch workspace" , e );
302- // 只有出错时才显示通知
303341 NotificationUtil .showError (I18nUtil .getMessage (MessageKeys .WORKSPACE_OPERATION_FAILED_DETAIL , e .getMessage ()));
304342 }
305343 }
@@ -368,6 +406,115 @@ private static JEditorPane getJEditorPane(String html) {
368406 return editorPane ;
369407 }
370408
409+
410+ /**
411+ * 创建 Git 工具栏面板
412+ * 根据工作区的 Git 配置情况动态显示不同的操作按钮
413+ *
414+ * @param workspace 当前 Git 工作区
415+ * @return Git 工具栏面板
416+ */
417+ private JPanel createGitToolbar (Workspace workspace ) {
418+ JPanel toolbar = new JPanel ();
419+ toolbar .setLayout (new BoxLayout (toolbar , BoxLayout .X_AXIS ));
420+ toolbar .setOpaque (false );
421+
422+ try {
423+ WorkspaceService workspaceService = WorkspaceService .getInstance ();
424+ RemoteStatus remoteStatus = workspaceService .getRemoteStatus (workspace .getId ());
425+
426+ // Commit 按钮(本地提交,始终显示)
427+ JButton commitButton = createGitButton (
428+ I18nUtil .getMessage (MessageKeys .WORKSPACE_GIT_COMMIT ),
429+ "icons/save.svg" ,
430+ e -> performGitOperation (workspace , GitOperation .COMMIT )
431+ );
432+ toolbar .add (commitButton );
433+
434+ // 远程操作按钮(仅在配置了远程仓库时显示)
435+ if (remoteStatus .hasRemote ) {
436+ // Pull 按钮(拉取远程更新)
437+ JButton pullButton = createGitButton (
438+ I18nUtil .getMessage (MessageKeys .WORKSPACE_GIT_PULL ),
439+ "icons/download.svg" ,
440+ e -> performGitOperation (workspace , GitOperation .PULL )
441+ );
442+ toolbar .add (pullButton );
443+
444+ // Push 按钮(仅在设置了上游分支时显示)
445+ if (remoteStatus .hasUpstream ) {
446+ JButton pushButton = createGitButton (
447+ I18nUtil .getMessage (MessageKeys .WORKSPACE_GIT_PUSH ),
448+ "icons/upload.svg" ,
449+ e -> performGitOperation (workspace , GitOperation .PUSH )
450+ );
451+ toolbar .add (pushButton );
452+ }
453+ }
454+
455+ } catch (Exception e ) {
456+ log .error ("Failed to create Git toolbar buttons" , e );
457+ }
458+
459+ return toolbar ;
460+ }
461+
462+ /**
463+ * 创建 Git 操作按钮
464+ */
465+ private JButton createGitButton (String tooltip , String iconPath , ActionListener action ) {
466+ JButton button = new JButton ();
467+ button .setIcon (new FlatSVGIcon (iconPath , 18 , 18 ));
468+ button .setToolTipText (tooltip );
469+ button .setFocusable (false );
470+ button .setBorderPainted (false );
471+ button .setContentAreaFilled (false );
472+ button .setPreferredSize (new Dimension (24 , 24 ));
473+ button .addActionListener (action );
474+
475+ // 添加鼠标悬停效果
476+ button .addMouseListener (new MouseAdapter () {
477+ @ Override
478+ public void mouseEntered (MouseEvent e ) {
479+ button .setContentAreaFilled (true );
480+ }
481+
482+ @ Override
483+ public void mouseExited (MouseEvent e ) {
484+ button .setContentAreaFilled (false );
485+ }
486+ });
487+
488+ return button ;
489+ }
490+
491+ /**
492+ * 执行 Git 操作(Commit/Pull/Push)
493+ *
494+ * @param workspace 目标工作区
495+ * @param operation Git 操作类型
496+ */
497+ private void performGitOperation (Workspace workspace , GitOperation operation ) {
498+ GitOperationDialog dialog = new GitOperationDialog (
499+ SwingUtilities .getWindowAncestor (this ),
500+ workspace ,
501+ operation
502+ );
503+ dialog .setVisible (true );
504+
505+ if (dialog .isConfirmed ()) {
506+ // Pull 操作后需要刷新相关面板以显示最新数据
507+ if (operation == GitOperation .PULL ) {
508+ SingletonFactory .getInstance (RequestCollectionsLeftPanel .class )
509+ .switchWorkspaceAndRefreshUI (SystemUtil .getCollectionPathForWorkspace (workspace ));
510+ SingletonFactory .getInstance (EnvironmentPanel .class )
511+ .switchWorkspaceAndRefreshUI (SystemUtil .getEnvPathForWorkspace (workspace ));
512+ }
513+
514+ log .info ("Git {} operation completed successfully" , operation .getDisplayName ());
515+ }
516+ }
517+
371518 /**
372519 * 检查更新
373520 */
0 commit comments