resize.go (1849B)
1 // Copyright 2015 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 tcell 16 17 import ( 18 "time" 19 ) 20 21 // EventResize is sent when the window size changes. 22 type EventResize struct { 23 t time.Time 24 ws WindowSize 25 } 26 27 // NewEventResize creates an EventResize with the new updated window size, 28 // which is given in character cells. 29 func NewEventResize(width, height int) *EventResize { 30 ws := WindowSize{ 31 Width: width, 32 Height: height, 33 } 34 return &EventResize{t: time.Now(), ws: ws} 35 } 36 37 // When returns the time when the Event was created. 38 func (ev *EventResize) When() time.Time { 39 return ev.t 40 } 41 42 // Size returns the new window size as width, height in character cells. 43 func (ev *EventResize) Size() (int, int) { 44 return ev.ws.Width, ev.ws.Height 45 } 46 47 // PixelSize returns the new window size as width, height in pixels. The size 48 // will be 0,0 if the screen doesn't support this feature 49 func (ev *EventResize) PixelSize() (int, int) { 50 return ev.ws.PixelWidth, ev.ws.PixelHeight 51 } 52 53 type WindowSize struct { 54 Width int 55 Height int 56 PixelWidth int 57 PixelHeight int 58 } 59 60 // CellDimensions returns the dimensions of a single cell, in pixels 61 func (ws WindowSize) CellDimensions() (int, int) { 62 if ws.PixelWidth == 0 || ws.PixelHeight == 0 { 63 return 0, 0 64 } 65 return (ws.PixelWidth / ws.Width), (ws.PixelHeight / ws.Height) 66 }