...

Source file src/github.com/gdamore/tcell/v2/views/sstextbar.go

Documentation: github.com/gdamore/tcell/v2/views

     1  // Copyright 2016 The Tcell Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use file except in compliance with the License.
     5  // You may obtain a copy of the license at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package views
    16  
    17  import (
    18  	"sync"
    19  
    20  	"github.com/gdamore/tcell/v2"
    21  )
    22  
    23  // SimpleStyledTextBar is a Widget that provides a single line of text, but
    24  // with distinct left, center, and right areas.  Each of the areas can be
    25  // styled differently, and can contain internal style markup.
    26  // They align to the left, center, and right respectively.
    27  // This is basically a convenience type on top SimpleStyledText and BoxLayout.
    28  type SimpleStyledTextBar struct {
    29  	left   *SimpleStyledText
    30  	right  *SimpleStyledText
    31  	center *SimpleStyledText
    32  	once   sync.Once
    33  
    34  	BoxLayout
    35  }
    36  
    37  // SetRight sets the right text for the textbar.
    38  // It is always right-aligned.
    39  func (s *SimpleStyledTextBar) SetRight(m string) {
    40  	s.initialize()
    41  	s.right.SetMarkup(m)
    42  }
    43  
    44  // SetLeft sets the left text for the textbar.
    45  // It is always left-aligned.
    46  func (s *SimpleStyledTextBar) SetLeft(m string) {
    47  	s.initialize()
    48  	s.left.SetMarkup(m)
    49  }
    50  
    51  // SetCenter sets the center text for the textbar.
    52  // It is always centered.
    53  func (s *SimpleStyledTextBar) SetCenter(m string) {
    54  	s.initialize()
    55  	s.center.SetMarkup(m)
    56  }
    57  
    58  func (s *SimpleStyledTextBar) RegisterRightStyle(r rune, style tcell.Style) {
    59  	s.initialize()
    60  	s.right.RegisterStyle(r, style)
    61  }
    62  
    63  func (s *SimpleStyledTextBar) RegisterLeftStyle(r rune, style tcell.Style) {
    64  	s.initialize()
    65  	s.left.RegisterStyle(r, style)
    66  }
    67  
    68  func (s *SimpleStyledTextBar) RegisterCenterStyle(r rune, style tcell.Style) {
    69  	s.initialize()
    70  	s.center.RegisterStyle(r, style)
    71  }
    72  
    73  func (s *SimpleStyledTextBar) Size() (int, int) {
    74  	s.initialize()
    75  	w, h := s.BoxLayout.Size()
    76  	if h < 1 {
    77  		h = 1
    78  	}
    79  	if w < 1 {
    80  		w = 1
    81  	}
    82  	return w, h
    83  }
    84  
    85  func (s *SimpleStyledTextBar) initialize() {
    86  	s.once.Do(func() {
    87  		s.center = NewSimpleStyledText()
    88  		s.left = NewSimpleStyledText()
    89  		s.right = NewSimpleStyledText()
    90  		s.center.SetAlignment(VAlignTop | HAlignCenter)
    91  		s.left.SetAlignment(VAlignTop | HAlignLeft)
    92  		s.right.SetAlignment(VAlignTop | HAlignRight)
    93  		s.BoxLayout.SetOrientation(Horizontal)
    94  		s.BoxLayout.AddWidget(s.left, 0.0)
    95  		s.BoxLayout.AddWidget(NewSpacer(), 1.0)
    96  		s.BoxLayout.AddWidget(s.center, 0.0)
    97  		s.BoxLayout.AddWidget(NewSpacer(), 1.0)
    98  		s.BoxLayout.AddWidget(s.right, 0.0)
    99  	})
   100  }
   101  
   102  // Init prepares the widget for use, ensuring resources needed are
   103  // allocated, etc.
   104  func (s *SimpleStyledTextBar) Init() {
   105  	s.initialize()
   106  }
   107  
   108  // NewSimpleStyledTextBar creates an empty, initialized TextBar.
   109  func NewSimpleStyledTextBar() *SimpleStyledTextBar {
   110  	s := &SimpleStyledTextBar{}
   111  	s.initialize()
   112  	return s
   113  }
   114  

View as plain text