github.com/gop9/olt@v0.0.0-20200202132135-d956aad50b08/framework/split.go (about)

     1  package framework
     2  
     3  import (
     4  	"github.com/gop9/olt/framework/rect"
     5  	nstyle "github.com/gop9/olt/framework/style"
     6  	"github.com/gop9/olt/gio/io/pointer"
     7  )
     8  
     9  type ScalableSplit struct {
    10  	Size     int
    11  	MinSize  int
    12  	Spacing  int
    13  	lastsize int
    14  	resize   bool
    15  }
    16  
    17  func (s *ScalableSplit) Horizontal(w *Window, bounds rect.Rect) (bounds0, bounds1 rect.Rect) {
    18  	scaling := w.Master().Style().Scaling
    19  
    20  	var rszbounds rect.Rect
    21  	bounds0, bounds1, rszbounds = s.horizontalnw(bounds, scaling)
    22  
    23  	w.LayoutSpacePushScaled(rszbounds)
    24  	rszbounds, _ = w.Custom(nstyle.WidgetStateInactive)
    25  
    26  	if w.Input().Mouse.IsClickDownInRect(pointer.ButtonLeft, rszbounds, true) {
    27  		s.resize = true
    28  	}
    29  	if s.resize {
    30  		if !w.Input().Mouse.Down(pointer.ButtonLeft) {
    31  			s.resize = false
    32  		} else {
    33  			s.Size += int(float64(w.Input().Mouse.Delta.Y) / scaling)
    34  			if s.Size <= s.MinSize {
    35  				s.Size = s.MinSize
    36  			}
    37  		}
    38  	}
    39  
    40  	return
    41  }
    42  
    43  func (s *ScalableSplit) horizontalnw(bounds rect.Rect, scaling float64) (bounds0, bounds1, rszbounds rect.Rect) {
    44  	if bounds.H < 0 || bounds.W < 0 {
    45  		return
    46  	}
    47  
    48  	if s.lastsize == 0 {
    49  		s.lastsize = bounds.H
    50  	}
    51  	if s.lastsize != bounds.H {
    52  		diff := int(float64(bounds.H-s.lastsize) / scaling)
    53  		s.Size += diff / 2
    54  		s.lastsize = bounds.H
    55  	}
    56  
    57  	hs := int(float64(s.Spacing) * scaling)
    58  	h := bounds.H - hs
    59  	var h0, h1 int
    60  	if s.Size == 0 {
    61  		h0 = h / 2
    62  		h1 = h - h0
    63  		s.Size = int(float64(h0) / scaling)
    64  	} else {
    65  		h0 = int(float64(s.Size) * scaling)
    66  		h1 = h - h0
    67  	}
    68  
    69  	minh := int(float64(s.MinSize) * scaling)
    70  	if h1 < minh {
    71  		h1 = minh
    72  		h0 = h - h1
    73  	}
    74  	if h0 < minh {
    75  		h0 = minh
    76  		h1 = h - h0
    77  	}
    78  
    79  	bounds0 = bounds
    80  	bounds0.H = h0
    81  
    82  	rszbounds = bounds
    83  	rszbounds.Y += bounds0.H
    84  	rszbounds.H = hs
    85  
    86  	bounds1 = bounds
    87  	bounds1.Y = rszbounds.Y + rszbounds.H
    88  	bounds1.H = h1
    89  
    90  	return bounds0, bounds1, rszbounds
    91  }
    92  
    93  func (s *ScalableSplit) Vertical(w *Window, bounds rect.Rect) (bounds0, bounds1 rect.Rect) {
    94  	scaling := w.Master().Style().Scaling
    95  
    96  	var rszbounds rect.Rect
    97  	bounds0, bounds1, rszbounds = s.verticalnw(bounds, scaling)
    98  
    99  	w.LayoutSpacePushScaled(rszbounds)
   100  	rszbounds, _ = w.Custom(nstyle.WidgetStateInactive)
   101  
   102  	if w.Input().Mouse.IsClickDownInRect(pointer.ButtonLeft, rszbounds, true) {
   103  		s.resize = true
   104  	}
   105  	if s.resize {
   106  		if !w.Input().Mouse.Down(pointer.ButtonLeft) {
   107  			s.resize = false
   108  		} else {
   109  			s.Size += int(float64(w.Input().Mouse.Delta.X) / scaling)
   110  			if s.Size <= s.MinSize {
   111  				s.Size = s.MinSize
   112  			}
   113  		}
   114  	}
   115  
   116  	return bounds0, bounds1
   117  }
   118  
   119  func (s *ScalableSplit) verticalnw(bounds rect.Rect, scaling float64) (bounds0, bounds1, rszbounds rect.Rect) {
   120  	if bounds.H < 0 || bounds.W < 0 {
   121  		return
   122  	}
   123  
   124  	if s.lastsize == 0 {
   125  		s.lastsize = bounds.W
   126  	}
   127  	if s.lastsize != bounds.W {
   128  		diff := int(float64(bounds.W-s.lastsize) / scaling)
   129  		s.Size += diff / 2
   130  		s.lastsize = bounds.W
   131  	}
   132  
   133  	ws := int(float64(s.Spacing) * scaling)
   134  	wt := bounds.W - ws
   135  	var w0, w1 int
   136  	if s.Size == 0 {
   137  		w0 = wt / 2
   138  		w1 = wt - w0
   139  		s.Size = int(float64(w0) / scaling)
   140  	} else {
   141  		w0 = int(float64(s.Size) * scaling)
   142  		w1 = wt - w0
   143  	}
   144  
   145  	minw := int(float64(s.MinSize) * scaling)
   146  	if w1 < minw {
   147  		w1 = minw
   148  		w0 = wt - w1
   149  	}
   150  	if w0 < minw {
   151  		w0 = minw
   152  		w1 = wt - w0
   153  	}
   154  
   155  	bounds0 = bounds
   156  	bounds0.W = w0
   157  
   158  	rszbounds = bounds
   159  	rszbounds.X += bounds0.W
   160  	rszbounds.W = ws
   161  
   162  	bounds1 = bounds
   163  	bounds1.X = rszbounds.X + rszbounds.W
   164  	bounds1.W = w1
   165  
   166  	return bounds0, bounds1, rszbounds
   167  }