tui: resize + src selection
authorNiki Roo <niki@nikiroo.be>
Fri, 8 Mar 2019 16:43:23 +0000 (17:43 +0100)
committerNiki Roo <niki@nikiroo.be>
Fri, 8 Mar 2019 16:43:23 +0000 (17:43 +0100)
src/be/nikiroo/fanfix/reader/tui/TSizeConstraint.java
src/be/nikiroo/fanfix/reader/tui/TuiReaderMainWindow.java
src/be/nikiroo/fanfix/reader/tui/TuiReaderStoryWindow.java

index 3baf1255892301f1421fc477bf9940d21bc66c73..859a6c35de170fd805a2d9e12b84850da02d97f5 100644 (file)
@@ -54,9 +54,9 @@ public class TSizeConstraint {
        }
 
        // coordinates < 0 = from the other side, x2 or y2 = 0 = max size
-       static void setSize(List<TSizeConstraint> list, TWidget child, Integer x1,
-                       Integer y1, Integer x2, Integer y2) {
-               list.add(new TSizeConstraint(child, x1, y1, x2, y2));
+       static void setSize(List<TSizeConstraint> sizeConstraints, TWidget child,
+                       Integer x1, Integer y1, Integer x2, Integer y2) {
+               sizeConstraints.add(new TSizeConstraint(child, x1, y1, x2, y2));
        }
 
        static void resize(List<TSizeConstraint> sizeConstraints) {
index f2167a6d3a1371026d96002640c99f90db0acdc5..126cee58071c5105c0bdda3959338d2cf114ac47 100644 (file)
@@ -2,20 +2,22 @@ package be.nikiroo.fanfix.reader.tui;
 
 import java.io.IOException;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.List;
 
 import jexer.TAction;
+import jexer.TComboBox;
 import jexer.TCommand;
 import jexer.TField;
 import jexer.TFileOpenBox.Type;
 import jexer.TKeypress;
+import jexer.TLabel;
 import jexer.TList;
 import jexer.TStatusBar;
 import jexer.TWindow;
 import jexer.event.TCommandEvent;
 import jexer.event.TKeypressEvent;
 import jexer.event.TMenuEvent;
+import jexer.event.TResizeEvent;
 import be.nikiroo.fanfix.Instance;
 import be.nikiroo.fanfix.data.MetaData;
 import be.nikiroo.fanfix.library.BasicLibrary;
@@ -39,6 +41,10 @@ class TuiReaderMainWindow extends TWindow {
        private Reader reader;
        private String source;
        private String filter = "";
+       private List<TSizeConstraint> sizeConstraints = new ArrayList<TSizeConstraint>();
+
+       // TODO: because no way to find out the current index!!
+       private TComboBox select;
 
        /**
         * Create a new {@link TuiReaderMainWindow} without any stories in the list.
@@ -56,47 +62,15 @@ class TuiReaderMainWindow extends TWindow {
                listKeys = new ArrayList<MetaData>();
                listItems = new ArrayList<String>();
 
-               // TODO size + onResize
-
-               addLabel("Search: ", 5, 3);
-               @SuppressWarnings("unused")
-               TField field = new TField(this, 15, 3, 5, true) {
-                       @Override
-                       public void onKeypress(TKeypressEvent keypress) {
-                               super.onKeypress(keypress);
-                               TKeypress key = keypress.getKey();
-                               if (key.isFnKey() && key.getKeyCode() == TKeypress.ENTER) {
-                                       TuiReaderMainWindow.this.filter = getText();
-                                       TuiReaderMainWindow.this.refreshStories();
-                               }
-                       }
-               };
-
-               addLabel("Sort by: ", 5, 1);
-               // -1 = no default index (0 means first,...) 1=height when visible, null
-               // = action
-               List<String> data = Arrays.asList("(show all)", "Source", "Name",
-                               "Author");
-               // must be last so to be able to draw over the rest
-               // TODO: make it so we cannot add manual entries
-               // TODO: how to select the item via keyboard? why double-click via
-               // mouse?
-               addComboBox(15, 1, 12, data, 0,
-                               Math.min(data.size() + 1, getHeight() - 1 - 1), null);
-
-               list = addList(listItems, 0, 7, getWidth(), getHeight(), new TAction() {
-                       @Override
-                       public void DO() {
-                               MetaData meta = getSelectedMeta();
-                               if (meta != null) {
-                                       readStory(meta);
-                               }
-                       }
-               });
+               addSearch();
+               addList();
+               addSelect(); // TODO: last so it can draw over the rest
 
                TStatusBar statusBar = reader.setStatusBar(this, "Library");
                statusBar.addShortcutKeypress(TKeypress.kbCtrlF, CMD_SEARCH, "Search");
 
+               TSizeConstraint.resize(sizeConstraints);
+
                // TODO: remove when not used anymore
 
                // addLabel("Label (1,1)", 1, 1);
@@ -122,6 +96,103 @@ class TuiReaderMainWindow extends TWindow {
                // root.addChild("child 2").addChild("sub child");
        }
 
+       private void addSearch() {
+               TLabel lblSearch = addLabel("Search: ", 0, 0);
+
+               TField search = new TField(this, 0, 0, 1, true) {
+                       @Override
+                       public void onKeypress(TKeypressEvent keypress) {
+                               super.onKeypress(keypress);
+                               TKeypress key = keypress.getKey();
+                               if (key.isFnKey() && key.getKeyCode() == TKeypress.ENTER) {
+                                       TuiReaderMainWindow.this.filter = getText();
+                                       TuiReaderMainWindow.this.refreshStories();
+                               }
+                       }
+               };
+
+               TSizeConstraint.setSize(sizeConstraints, lblSearch, 5, 1, null, null);
+               TSizeConstraint.setSize(sizeConstraints, search, 15, 1, -5, null);
+       }
+
+       private void addList() {
+               list = addList(listItems, 0, 0, 10, 10, new TAction() {
+                       @Override
+                       public void DO() {
+                               MetaData meta = getSelectedMeta();
+                               if (meta != null) {
+                                       readStory(meta);
+                               }
+                       }
+               });
+
+               TSizeConstraint.setSize(sizeConstraints, list, 0, 7, 0, 0);
+       }
+
+       private void addSelect() {
+               // TODO: make a full list
+               final List<String> options = new ArrayList<String>();
+               options.add("(all opt)");
+               options.add("Sources");
+               options.add("Author");
+
+               // TODO
+               final List<String> sources = new ArrayList<String>();
+               sources.add("(show all)");
+               for (String source : reader.getLibrary().getSources()) {
+                       sources.add(source);
+               }
+
+               TLabel lblSelect = addLabel("Select: ", 0, 0);
+
+               // TODO: why must be last so to be able to draw over the rest
+               // TODO: make it so we cannot add manual entries
+               // TODO: how to select the item via keyboard? why double-click via
+               // mouse?
+               // TODO: how to change the values size on resize?
+               // TODO: setWidth() does not impact the display width, only the control
+               // and the down arrow on the right
+               // TODO: width 1 +resize + click on down arrow = bad format exception
+               select = addComboBox(0, 0, 10, sources, 0,
+                               Math.min(sources.size() + 1, getHeight() - 1 - 1),
+                               new TAction() {
+                                       @Override
+                                       public void DO() {
+                                               // TODO: wut?
+                                               if (select.getText().equals("(show all)")) {
+                                                       setSource(null);
+                                               } else {
+                                                       setSource(select.getText());
+                                               }
+                                               refreshStories();
+                                       }
+                               });
+
+               final TComboBox option = addComboBox(0, 0, 10, options, 0,
+                               Math.min(sources.size() + 1, getHeight() - 1 - 1),
+                               new TAction() {
+                                       @Override
+                                       public void DO() {
+                                               // TODO Not working:
+                                               sources.clear();
+                                               sources.add("(show all)");
+                                               for (String source : reader.getLibrary().getSources()) {
+                                                       sources.add(source);
+                                               }
+                                       }
+                               });
+
+               TSizeConstraint.setSize(sizeConstraints, lblSelect, 5, 3, null, null);
+               TSizeConstraint.setSize(sizeConstraints, option, 15, 3, -5, null);
+               TSizeConstraint.setSize(sizeConstraints, select, 15, 4, -5, null);
+       }
+
+       @Override
+       public void onResize(TResizeEvent resize) {
+               super.onResize(resize);
+               TSizeConstraint.resize(sizeConstraints);
+       }
+
        @Override
        public void onClose() {
                setVisible(false);
index ddf7ad13d98e35a0d47e1984881067aa44131c8b..d4fac73a166594be5ee5d25ff8dde9ab6ddbc922 100644 (file)
@@ -119,11 +119,7 @@ class TuiReaderStoryWindow extends TWindow {
                textField.getVerticalScroller().setX(
                                textField.getVerticalScroller().getX() + 1);
 
-               String name = currentChapter.getLabel();
-               int size = Math.max(name.length(), currentChapter.getWidth());
-               name = String.format("%" + size + "s", name);
-
-               currentChapter.setLabel(name);
+               setCurrentChapterText();
        }
 
        /**
@@ -303,10 +299,7 @@ class TuiReaderStoryWindow extends TWindow {
                }
 
                int width = getWidth() - currentChapter.getX();
-               while (name.length() < width) {
-                       name += " ";
-               }
-
+               name = String.format("%-" + width + "s", name);
                if (name.length() > width) {
                        name = name.substring(0, width);
                }