github.com/oam-dev/kubevela@v1.9.11/references/cli/top/component/page.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  
    22  	"github.com/rivo/tview"
    23  
    24  	"github.com/oam-dev/kubevela/references/cli/top/model"
    25  )
    26  
    27  // Pages is the app's main content view component
    28  type Pages struct {
    29  	*tview.Pages
    30  	*model.Stack
    31  }
    32  
    33  // NewPages return a page component
    34  func NewPages() *Pages {
    35  	p := &Pages{
    36  		Pages: tview.NewPages(),
    37  		Stack: model.NewStack(),
    38  	}
    39  	p.Stack.AddListener(p)
    40  	return p
    41  }
    42  
    43  // Init table component
    44  func (p *Pages) Init() {}
    45  
    46  // Name return pages' name
    47  func (p *Pages) Name() string {
    48  	return "Page"
    49  }
    50  
    51  // Start table component
    52  func (p *Pages) Start() {}
    53  
    54  // Stop table component
    55  func (p *Pages) Stop() {}
    56  
    57  // Hint return key action menu hints of the component
    58  func (p *Pages) Hint() []model.MenuHint {
    59  	return []model.MenuHint{}
    60  }
    61  
    62  // StackPop change itself when accept "pop" notify from app's main view
    63  func (p *Pages) StackPop(old, _ model.View) {
    64  	p.delete(old)
    65  }
    66  
    67  // StackPush change itself when accept "push" notify from app's main view
    68  func (p *Pages) StackPush(_, new model.View) {
    69  	p.addAndShow(new)
    70  }
    71  
    72  // AddAndShow adds a new page and bring it to front.
    73  func (p *Pages) addAndShow(c model.View) {
    74  	p.add(c)
    75  	p.show(c)
    76  }
    77  
    78  func (p *Pages) add(c model.View) {
    79  	p.AddPage(componentID(c), c, true, true)
    80  }
    81  
    82  func (p *Pages) delete(c model.View) {
    83  	p.RemovePage(componentID(c))
    84  }
    85  
    86  func (p *Pages) show(c model.View) {
    87  	p.SwitchToPage(componentID(c))
    88  }
    89  
    90  func componentID(c model.View) string {
    91  	if c.Name() == "" {
    92  		panic("View has no name")
    93  	}
    94  	return fmt.Sprintf("%s-%p", c.Name(), c)
    95  }