Skip to content

Commit

Permalink
added auto column sizes for tables
Browse files Browse the repository at this point in the history
  • Loading branch information
Osiris-Team committed Jun 26, 2023
1 parent 1c3d297 commit d8991e0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/osiris/desku/ui/display/RTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public RTable headers(Class<?> clazz, Predicate<Field> predicate) {
for (Field f : clazz.getFields()) {
if (predicate.test(f)) {
String name = f.getName();
table.headers.add(new Table.Header().add(new Text(name)));
table.headers.add(new Text(name));
}
}
table.recalcMaxColumnWidthPercent();
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/osiris/desku/ui/display/Table.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
border: 1px solid #ddd;
padding: 4px;
width: 100%;
flex-grow: 1;
align-items: center;
}
.desku-table-row *, .desku-table-header * {
.desku-table-cell{
justify-content: center;
align-items: center;
flex-grow: 1;
Expand Down
69 changes: 42 additions & 27 deletions src/main/java/com/osiris/desku/ui/display/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.osiris.events.Event;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class Table extends Component<Table> {
Expand All @@ -16,13 +18,15 @@ public class Table extends Component<Table> {
}
}

public Headers headers = new Headers(this);
public Row headers = new Row().addClass("desku-table-header");
public Rows rows = new Rows(this);
/**
* Gets recalculated in {@link #headers(Header...)}.
* Gets recalculated in {@link #headers(Row...)}. <br>
* Set to -1 or smaller, to disable.
*/
public double maxColumnWidthPercent = 100;
public void recalcMaxColumnWidthPercent(){
if(maxColumnWidthPercent < 0) return;
maxColumnWidthPercent = (1.0 / headers.children.size()) * 100.0;
for (Component<?> header : headers.children) {
header.putStyle("max-width", maxColumnWidthPercent+"%");
Expand All @@ -38,7 +42,7 @@ public Table() {
add(headers, rows);
addClass("desku-table");
rows.onAddedChild.addAction(e -> {
if(e.childComp instanceof Row){
if(maxColumnWidthPercent > 0 && e.childComp instanceof Row){
Row row = (Row) e.childComp;
for (Component<?> rowColumn : row.children) {
rowColumn.putStyle("max-width", maxColumnWidthPercent+"%");
Expand All @@ -54,7 +58,7 @@ public Table() {
public Table headers(String... headers) {
this.headers.removeAll();
for (String header : headers) {
this.headers.add(new Header().add(new Text(header)));
this.headers.add(new Text(header));
}
recalcMaxColumnWidthPercent();
return _this;
Expand All @@ -63,9 +67,9 @@ public Table headers(String... headers) {
/**
* Easily set/replace the headers.
*/
public Table headers(Header... headers) {
public Table headers(Component<?>... headers) {
this.headers.removeAll();
for (Header header : headers) {
for (Component<?> header : headers) {
this.headers.add(header);
}
recalcMaxColumnWidthPercent();
Expand Down Expand Up @@ -118,33 +122,33 @@ public Table row(int index, Row newRow) {
return this;
}

public Header getHeaderAt(int index){
return (Header) headers.children.get(index);
public Component<?> getHeaderAt(int index){
return headers.children.get(index);
}

public static class Headers extends Component<Headers> {
/**
* Reference to parent table if needed for method chaining.
*/
public final Table t;
public Event<Header> _onHeaderClick = new Event<>();
public Event<Row> _onHeaderClick = new Event<>();

public Headers(Table table) {
super("headers");
this.t = table;
width("100%");
Consumer<AddedChildEvent> superAdd = this._add;
this._add = e -> {
if(e.isFirstAdd && e.childComp instanceof Header){
if(e.isFirstAdd && e.childComp instanceof Row){
e.childComp.onClick(click -> {
_onHeaderClick.execute((Header) e.childComp);
_onHeaderClick.execute((Row) e.childComp);
});
}
superAdd.accept(e);
};
}

public Headers onHeaderClick(Consumer<Header> code){
public Headers onHeaderClick(Consumer<Row> code){
_onHeaderClick.addAction((event) -> code.accept(event));
return this;
}
Expand Down Expand Up @@ -182,27 +186,38 @@ public Rows onRowClick(Consumer<Row> code){
public static class Row extends Component<Row> {
public Row() {
addClass("desku-table-row");
}
}

public static class Header extends Component<Header> {
/**
* Width and height styles of this header are
* also used for each of its rows.
*/
private boolean isForwardSizeToRows = true;
// Wrap all added children first into cell
Consumer<AddedChildEvent> superAdd = _add;
_add = e -> {
Cell cell = new Cell();
cell.add(e.childComp);
e.childComp = cell;
superAdd.accept(e);
};
}

public Header() {
addClass("desku-table-header");
public List<Cell> getCells(){
List<Cell> cells = new ArrayList<>();
for (Component<?> child : children) {
cells.add((Cell) child);
}
return cells;
}

public boolean isForwardSizeToRows(){
return isForwardSizeToRows;
public List<Component<?>> getContents(){
List<Component<?>> contents = new ArrayList<>();
for (Cell cell : getCells()) {
contents.add(cell.getContent());
}
return contents;
}
}

public Header forwardSizeToRows(boolean b){
this.isForwardSizeToRows = b;
return this;
public static class Cell extends Component<Cell> {

public Cell(){
addClass("desku-table-cell");
}

public Component<?> getContent(){
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/osiris/desku/ui/input/FileChooser.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ public void setDir(File dir) {
removeAll();

table = new Table();
table.maxColumnWidthPercent = -1;
add(table);
table.headers("Select", "Icon", dir == null ? ".." : // If this the case then parent shows drives/roots
dir.getAbsolutePath().replace("\\", "/"), "Modified");
Expand Down

0 comments on commit d8991e0

Please sign in to comment.