| 1 | /* |
| 2 | * This file is part of lanterna (http://code.google.com/p/lanterna/). |
| 3 | * |
| 4 | * lanterna is free software: you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Lesser General Public License as published by |
| 6 | * the Free Software Foundation, either version 3 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public License |
| 15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | * |
| 17 | * Copyright (C) 2010-2015 Martin |
| 18 | */ |
| 19 | package com.googlecode.lanterna.terminal; |
| 20 | |
| 21 | import com.googlecode.lanterna.TerminalSize; |
| 22 | |
| 23 | /** |
| 24 | * This class is a simple implementation of Terminal.ResizeListener which will keep track of the size of the terminal |
| 25 | * and let you know if the terminal has been resized since you last checked. This can be useful to avoid threading |
| 26 | * problems with the resize callback when your application is using a main event loop. |
| 27 | * |
| 28 | * @author martin |
| 29 | */ |
| 30 | @SuppressWarnings("WeakerAccess") |
| 31 | public class SimpleTerminalResizeListener implements ResizeListener { |
| 32 | |
| 33 | boolean wasResized; |
| 34 | TerminalSize lastKnownSize; |
| 35 | |
| 36 | /** |
| 37 | * Creates a new SimpleTerminalResizeListener |
| 38 | * @param initialSize Before any resize event, this listener doesn't know the size of the terminal. By supplying a |
| 39 | * value here, you control what getLastKnownSize() will return if invoked before any resize events has reached us. |
| 40 | */ |
| 41 | public SimpleTerminalResizeListener(TerminalSize initialSize) { |
| 42 | this.wasResized = false; |
| 43 | this.lastKnownSize = initialSize; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Checks if the terminal was resized since the last time this method was called. If this is the first time calling |
| 48 | * this method, the result is going to be based on if the terminal has been resized since this listener was attached |
| 49 | * to the Terminal. |
| 50 | * |
| 51 | * @return true if the terminal was resized, false otherwise |
| 52 | */ |
| 53 | public synchronized boolean isTerminalResized() { |
| 54 | if(wasResized) { |
| 55 | wasResized = false; |
| 56 | return true; |
| 57 | } |
| 58 | else { |
| 59 | return false; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Returns the last known size the Terminal is supposed to have. |
| 65 | * |
| 66 | * @return Size of the terminal, as of the last resize update |
| 67 | */ |
| 68 | public TerminalSize getLastKnownSize() { |
| 69 | return lastKnownSize; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public synchronized void onResized(Terminal terminal, TerminalSize newSize) { |
| 74 | this.wasResized = true; |
| 75 | this.lastKnownSize = newSize; |
| 76 | } |
| 77 | |
| 78 | } |