github.com/oam-dev/kubevela@v1.9.11/references/cli/top/component/theme_selector.go (about) 1 /* 2 Copyright 2022 The KubeVela Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package component 18 19 import ( 20 "fmt" 21 "sort" 22 23 "github.com/rivo/tview" 24 25 "github.com/oam-dev/kubevela/references/cli/top/config" 26 ) 27 28 // ThemeSelector is used to select the theme 29 type ThemeSelector struct { 30 Frame *tview.Frame 31 list *tview.List 32 style *config.ThemeConfig 33 closeFunc func() 34 } 35 36 // NewThemeSelector is used to create a new theme selector 37 func NewThemeSelector(config *config.ThemeConfig, closeFun func()) *ThemeSelector { 38 s := &ThemeSelector{ 39 style: config, 40 closeFunc: closeFun, 41 } 42 s.list = tview.NewList() 43 s.Frame = tview.NewFrame(s.list) 44 return s 45 } 46 47 // Init is used to initialize the theme selector 48 func (s *ThemeSelector) Init() { 49 s.Frame.SetBorder(true) 50 s.Frame.SetBorderColor(s.style.Border.Table.Color()) 51 s.Frame.SetBorders(2, 2, 2, 2, 4, 4) 52 s.Frame.AddText(themeSelectTip(), true, tview.AlignCenter, s.style.Info.Title.Color()) 53 s.Frame.SetTitle(fmt.Sprintf("[ %s ]", "Select Theme")) 54 s.Frame.SetTitleColor(s.style.Table.Title.Color()) 55 56 s.list.SetShortcutColor(s.style.Info.Title.Color()) 57 s.list.SetMainTextColor(s.style.Info.Text.Color()) 58 } 59 60 // Start is used to start the theme selector 61 func (s *ThemeSelector) Start() { 62 sort.Strings(config.ThemeNameArray) 63 for _, key := range config.ThemeNameArray { 64 s.list.AddItem(key, "", '*', s.selectedFunc(key)) 65 } 66 } 67 68 func (s *ThemeSelector) selectedFunc(theme string) func() { 69 return func() { 70 config.PersistentThemeConfig(theme) 71 s.closeFunc() 72 } 73 } 74 75 func themeSelectTip() string { 76 return "Restart vela top to take effect" 77 }