1717import javafx .beans .Observable ;
1818import javafx .fxml .FXMLLoader ;
1919import javafx .scene .Parent ;
20+ import javafx .scene .control .CheckMenuItem ;
21+ import javafx .scene .control .ContextMenu ;
2022import javafx .scene .control .TabPane ;
2123import javafx .scene .control .TextField ;
2224import javafx .scene .layout .VBox ;
2729@ Slf4j
2830public class NewLauncherController {
2931
32+ public static final String FAVORITE_CATEGORY_NAME = "置顶" ;
33+
3034 public VBox pluginCategories ;
3135
3236 public WebView startWebView ;
@@ -35,24 +39,58 @@ public class NewLauncherController {
3539
3640 public TextField txtSearch ;
3741
42+ private ContextMenu itemContextMenu ;
43+
3844 // 实现搜索用
3945 private List <PluginItemController > pluginItemControllers = new ArrayList <>();
4046
4147 public void initialize () {
4248 NewLauncherService .getInstance ().setController (this );
4349 txtSearch .textProperty ().addListener (this ::onSearchKeywordChanged );
50+ initContextMenu ();
4451 loadPlugins (); // 加载插件列表到界面上
4552 startWebView .getEngine ().load (IndexController .QQ_URL ); // 额外再打开一个反馈页面,可关闭
4653 }
4754
55+ private void initContextMenu () {
56+ CheckMenuItem chkFavorite = new CheckMenuItem ("置顶" );
57+ chkFavorite .setStyle ("-fx-padding: 0 35 0 0" );
58+
59+ this .itemContextMenu = new ContextMenu (chkFavorite );
60+
61+ chkFavorite .setOnAction (event -> {
62+ CheckMenuItem _this = (CheckMenuItem ) event .getSource ();
63+ PluginItemController pluginItemController =
64+ NewLauncherService .getInstance ().getCurrentPluginItem ();
65+ setFavorite (pluginItemController , _this .isSelected ());
66+ });
67+ }
68+
4869 public void onSearchKeywordChanged (Observable ob , String _old , String keyword ) {
4970 boolean notSearching = StringUtils .isBlank (keyword );
5071 pluginItemControllers .forEach (itemController -> {
5172 itemController .setVisible (notSearching || itemController .matchKeyword (keyword ));
5273 });
5374 }
5475
76+ private void setFavorite (PluginItemController itemController , boolean isFavorite ) {
77+ if (itemController == null ) {
78+ return ;
79+ }
80+
81+ itemController .getPluginJarInfo ().setIsFavorite (isFavorite );
82+ PluginManager .getInstance ().saveToFileQuietly ();
83+ loadPlugins ();
84+ }
85+
86+ /**
87+ * 加载/刷新插件列表
88+ */
5589 private void loadPlugins () {
90+
91+ this .pluginCategories .getChildren ().clear ();
92+ this .pluginItemControllers .clear ();
93+
5694 List <PluginJarInfo > pluginList = PluginManager .getInstance ().getPluginList ();
5795 ResourceBundle menuResourceBundle = Main .RESOURCE_BUNDLE ;
5896
@@ -61,7 +99,10 @@ private void loadPlugins() {
6199 for (PluginJarInfo jarInfo : pluginList ) {
62100 String menuParentTitle = jarInfo .getMenuParentTitle ();
63101 if (menuParentTitle != null ) {
64- String categoryName = menuResourceBundle .getString (menuParentTitle );
102+
103+ String categoryName = jarInfo .getIsFavorite ()?
104+ FAVORITE_CATEGORY_NAME : menuResourceBundle .getString (menuParentTitle );
105+
65106 PluginCategoryController category = categoryControllers .computeIfAbsent (
66107 categoryName , __ -> {
67108 PluginCategoryController _category =
@@ -72,6 +113,7 @@ private void loadPlugins() {
72113 );
73114
74115 PluginItemController item = PluginItemController .newInstance (jarInfo );
116+ item .setContextMenu (itemContextMenu );
75117 category .addItem (item );
76118
77119 if (!pluginItemControllers .contains (item )) {
@@ -82,7 +124,11 @@ private void loadPlugins() {
82124 }
83125
84126 private void addCategory (PluginCategoryController category ) {
85- this .pluginCategories .getChildren ().add (category .root );
127+ if (category .lblCategoryName .getText ().equals (FAVORITE_CATEGORY_NAME )) {
128+ this .pluginCategories .getChildren ().add (0 , category .root );
129+ } else {
130+ this .pluginCategories .getChildren ().add (category .root );
131+ }
86132 }
87133
88134 public TabPane getTabPane () {
0 commit comments