github.com/oam-dev/kubevela@v1.9.11/references/cli/top/component/app.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  	"github.com/gdamore/tcell/v2"
    21  	"github.com/rivo/tview"
    22  
    23  	"github.com/oam-dev/kubevela/references/cli/top/config"
    24  	"github.com/oam-dev/kubevela/references/cli/top/model"
    25  )
    26  
    27  // App represent the ui of application
    28  type App struct {
    29  	*tview.Application
    30  	actions    model.KeyActions
    31  	components map[string]tview.Primitive
    32  	Main       *tview.Pages
    33  	style      *config.ThemeConfig
    34  }
    35  
    36  // NewApp return the ui of application
    37  func NewApp(themeConfig *config.ThemeConfig) *App {
    38  	a := &App{
    39  		Application: tview.NewApplication(),
    40  		actions:     make(model.KeyActions),
    41  		Main:        tview.NewPages(),
    42  		style:       themeConfig,
    43  	}
    44  	a.components = map[string]tview.Primitive{
    45  		"info":   NewInfo(a.style),
    46  		"menu":   NewMenu(a.style),
    47  		"logo":   NewLogo(a.style),
    48  		"crumbs": NewCrumbs(a.style),
    49  	}
    50  	return a
    51  }
    52  
    53  // Init the ui of application init
    54  func (a *App) Init() {
    55  	a.SetRoot(a.Main, true)
    56  }
    57  
    58  // HasAction judge whether the key has the corresponding action
    59  func (a *App) HasAction(key tcell.Key) (model.KeyAction, bool) {
    60  	action, ok := a.actions[key]
    61  	return action, ok
    62  }
    63  
    64  // AddAction add a new keyAction
    65  func (a *App) AddAction(actions model.KeyActions) {
    66  	a.actions.Add(actions)
    67  }
    68  
    69  // DelAction delete a keyAction
    70  func (a *App) DelAction(keys []tcell.Key) {
    71  	a.actions.Delete(keys)
    72  }
    73  
    74  // QueueUpdate queues up a ui action.
    75  func (a *App) QueueUpdate(f func()) {
    76  	if a.Application == nil {
    77  		return
    78  	}
    79  	go func() {
    80  		a.Application.QueueUpdate(f)
    81  	}()
    82  }
    83  
    84  // QueueUpdateDraw queues up a ui action and redraw the ui.
    85  func (a *App) QueueUpdateDraw(f func()) {
    86  	if a.Application == nil {
    87  		return
    88  	}
    89  	go func() {
    90  		a.Application.QueueUpdateDraw(f)
    91  	}()
    92  }
    93  
    94  // Components return the application root components.
    95  func (a *App) Components() map[string]tview.Primitive {
    96  	return a.components
    97  }
    98  
    99  // Logo return logo component
   100  func (a *App) Logo() *Logo {
   101  	return a.components["logo"].(*Logo)
   102  }
   103  
   104  // Menu return key action menu component
   105  func (a *App) Menu() *Menu {
   106  	return a.components["menu"].(*Menu)
   107  }
   108  
   109  // Crumbs return the crumbs component
   110  func (a *App) Crumbs() *Crumbs {
   111  	return a.components["crumbs"].(*Crumbs)
   112  }
   113  
   114  // InfoBoard return system info component
   115  func (a *App) InfoBoard() *InfoBoard {
   116  	return a.Components()["info"].(*InfoBoard)
   117  }