Skip to content

Commit

Permalink
Updated to v0.14-b2.
Browse files Browse the repository at this point in the history
  • Loading branch information
23rd committed Jan 1, 2021
2 parents 2a57dfa + 49659c5 commit 081ac6b
Show file tree
Hide file tree
Showing 19 changed files with 473 additions and 115 deletions.
2 changes: 1 addition & 1 deletion src/chatty/SettingsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ public void defineSettings() {

// Popouts
settings.addBoolean("popoutSaveAttributes", true);
settings.addBoolean("popoutCloseLastChannel", true);
settings.addBoolean("popoutCloseLastChannel", false);
settings.addList("popoutAttributes", new ArrayList(), Setting.STRING);

// Titlebar
Expand Down
2 changes: 2 additions & 0 deletions src/chatty/TwitchClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -1424,6 +1424,8 @@ private void testCommands(Room room, String command, String parameter) {
for (String chan : split2) {
g.printLine(c.getUser(chan, "test").getRoom(), "test");
}
} else if (command.equals("switchchan")) {
g.switchToChannel(parameter);
} else if (command.equals("settestuser")) {
String[] split = parameter.split(" ");
createTestUser(split[0], split[1]);
Expand Down
203 changes: 166 additions & 37 deletions src/chatty/gui/Channels.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import chatty.util.dnd.DockContent;
import chatty.util.dnd.DockListener;
import chatty.util.dnd.DockManager;
import chatty.util.dnd.DockPath;
import chatty.util.dnd.DockPathEntry;
import chatty.util.dnd.DockSetting;
import java.awt.Component;
import java.awt.Dimension;
Expand Down Expand Up @@ -94,8 +96,6 @@ public Channels(MainGui gui, StyleManager styleManager,
dock = new DockManager(new DockListener() {
@Override
public void activeContentChanged(DockPopout window, DockContent content, boolean focusChange) {
//System.out.println("changed: "+content+" "+Debugging.getStacktrace());
channelChanged();
if (content.getComponent() instanceof Channel) {
Channel c = (Channel) content.getComponent();
lastActiveChannel = c;
Expand All @@ -105,9 +105,21 @@ public void activeContentChanged(DockPopout window, DockContent content, boolean
setInitialFocus(c);
}
}
// This also resets lastActiveChannel if necessary
updateActiveContent();
resetTab(content);
Debugging.println("dndp", "Path: %s", content.getPath());
/**
* Only inform listener if there is at least one channel and the
* main isn't empty. This is to prevent stuff from trying to
* update when the last overall channel is removed and the last
* content is popped out and the default channel has not been
* added yet. Let what handles the default channel call the
* listener.
*/
if ((!channels.isEmpty() || defaultChannel != null) && !dock.isMainEmpty()) {
channelChanged();
}
Debugging.println("dnda", "Path: %s", content.getPath());
}

@Override
Expand All @@ -119,6 +131,16 @@ public void popoutOpened(DockPopout popout, DockContent content) {
public void popoutClosed(DockPopout popout, List<DockContent> contents) {
dockPopoutClosed(popout, contents);
}

@Override
public void contentAdded(DockContent content) {
checkDefaultChannel();
}

@Override
public void contentRemoved(DockContent content) {
checkDefaultChannel();
}

});
// One-time settings
Expand Down Expand Up @@ -223,14 +245,20 @@ public void updateRoom(Room room) {
}
}

/**
* Update tabs to show which one is active. Also implicitly (by calling
* getActiveChannel()) resets lastACtiveChannel is necessary.
*/
private void updateActiveContent() {
if (!doesChannelExist(lastActiveChannel)) {
resetLastActiveChannel();
}
Channel active = getActiveChannel();
for (Channel chan : channels.values()) {
if (getActiveChannel() == chan) {
chan.getDockContent().setActive(true);
}
else {
chan.getDockContent().setActive(false);
}
chan.getDockContent().setActive(active == chan);
}
if (defaultChannel != null) {
defaultChannel.getDockContent().setActive(active == defaultChannel);
}
}

Expand Down Expand Up @@ -353,6 +381,14 @@ else if (panel.setRoom(room)) {
*/
private void addDefaultChannel() {
defaultChannel = createChannel(Room.EMPTY, Channel.Type.NONE);
addDefaultChannelToDock();
}

private void addDefaultChannelToDock() {
// Ensure that it's being added in main window
DockPath path = new DockPath(defaultChannel.getDockContent());
path.addParent(DockPathEntry.createPopout(null));
defaultChannel.getDockContent().setTargetPath(path);
dock.addContent(defaultChannel.getDockContent());
}

Expand All @@ -375,19 +411,21 @@ public Channel addChannel(Room room, Channel.Type type) {
defaultChannel = null;
panel.setType(type);
panel.setRoom(room);
channelChanged();
channels.put(room.getChannel(), panel);
}
else {
// No default channel, so create a new one
panel = createChannel(room, type);
// Add to channels first so it's already known as "existing"
channels.put(room.getChannel(), panel);
dock.addContent(panel.getDockContent());
if (type != Channel.Type.WHISPER) {
dock.setActiveContent(panel.getDockContent());
}
}
channels.put(room.getChannel(), panel);
// Update after it has been added to "channels"
updateActiveContent();
channelChanged();
return panel;
}

Expand Down Expand Up @@ -423,16 +461,52 @@ public void removeChannel(final String channelName) {
return;
}
channels.remove(channelName);
// Removing will automatically run checkDefaultChannel()
dock.removeContent(channel.getDockContent());
channel.cleanUp();

if (dock.isMainEmpty() || channels.isEmpty()) {
if (!closeLastChannelPopout || !dock.closeWindow()) {
}

/**
* Add/remove/move default channel depending on whether the main window is
* empty and any other channels are presently added.
*/
private void checkDefaultChannel() {
if (defaultChannel == null) {
/**
* Currently no default channel, check if it should be added
*/
if (channels.isEmpty()) {
addDefaultChannel();
}
channelChanged();
gui.updateState();
else if (dock.isMainEmpty()) {
if (!closeLastChannelPopout || !dock.closePopout()) {
addDefaultChannel();
}
}
}
else {
/**
* Default channel exists, check if it should be removed/moved. In
* some cases this may be called again when it's already removed, so
* also check that's still in there, just to be safe.
*/
if (dock.hasContent(defaultChannel.getDockContent())) {
if (!channels.isEmpty() && dock.getContents(null).size() > 1) {
dock.removeContent(defaultChannel.getDockContent());
defaultChannel.cleanUp();
defaultChannel = null;
}
// If main is empty, move default channel to main
else if (dock.isMainEmpty()) {
// Removing content will trigger this method as well, but this
// is only run when the default channel is still added
dock.removeContent(defaultChannel.getDockContent());
addDefaultChannelToDock();
}
}
}
updateActiveContent();
channelChanged();
}

//==========================
Expand All @@ -446,6 +520,9 @@ public void removeChannel(final String channelName) {
* @param window Open in window instead of dialog
*/
public void popout(DockContent content, boolean window) {
if (!content.canPopout()) {
return;
}
/**
* Setting the location/size should only be done for popouts not created
* by drag, so not in dockPopoutOpened()
Expand Down Expand Up @@ -473,6 +550,7 @@ public void popout(DockContent content, boolean window) {
private void dockPopoutOpened(DockPopout popout) {
// Register hotkeys if necessary
gui.popoutCreated(popout.getWindow());
checkDefaultChannel();
}

private void dockPopoutClosed(DockPopout popout, List<DockContent> contents) {
Expand All @@ -481,13 +559,15 @@ private void dockPopoutClosed(DockPopout popout, List<DockContent> contents) {
dialogsAttributes.add(0, new LocationAndSize(
window.getLocation(), window.getSize()));
}
if (!contents.isEmpty()) {
if (defaultChannel != null) {
dock.removeContent(defaultChannel.getDockContent());
defaultChannel = null;
}
gui.updateState(true);
}
// if (!contents.isEmpty() && defaultChannel != null
// && !contents.contains(defaultChannel.getDockContent())
// && !channels.isEmpty()) {
// dock.removeContent(defaultChannel.getDockContent());
// defaultChannel.cleanUp();
// defaultChannel = null;
// }
checkDefaultChannel();
gui.updateState(true);
}

//--------------------------
Expand Down Expand Up @@ -579,7 +659,8 @@ private void channelChanged() {
*
* <p>
* If the focus is not on a window containing a Channel, then the current
* tab of the main window is returned.
* tab of the main window is returned. If no Channel currently has focus, a
* channel is selected from the existing channels.
* </p>
*
* @return The Channel object which is currently selected, could be null,
Expand All @@ -592,16 +673,33 @@ public Channel getActiveChannel() {
if (activeContent instanceof DockChannelContainer) {
return ((DockChannelContainer)activeContent).getContent();
}
if (channels.containsValue(lastActiveChannel) || lastActiveChannel == defaultChannel) {
if (doesChannelExist(lastActiveChannel)) {
return lastActiveChannel;
}
// If a Channel isn't active, try others
for (DockContent otherActive : dock.getAllActive().values()) {
if (otherActive instanceof DockChannelContainer) {
return ((DockChannelContainer)otherActive).getContent();
// Checked before, so must not be valid
resetLastActiveChannel();
return lastActiveChannel;
}

private void resetLastActiveChannel() {
lastActiveChannel = null;
// Find new channel
if (!channels.isEmpty()) {
// Try visible first (selected tab, popout)
for (Channel chan : channels.values()) {
if (dock.isContentVisible(chan.getDockContent())) {
lastActiveChannel = chan;
break;
}
}
if (lastActiveChannel == null) {
lastActiveChannel = channels.values().iterator().next();
}
}
return null;
else {
lastActiveChannel = defaultChannel;
}
Debugging.println("dnda", "Reset last active channel to: %s", lastActiveChannel);
}

/**
Expand Down Expand Up @@ -639,7 +737,7 @@ public Channel getMainActiveChannel() {
}

public DockContent getMainActiveContent() {
return dock.getAllActive().get(null);
return dock.getActiveContent(null);
}

/**
Expand All @@ -649,9 +747,10 @@ public DockContent getMainActiveContent() {
*/
public Map<DockPopout, DockContent> getActivePopoutContent() {
Map<DockPopout, DockContent> result = new HashMap<>();
for (Map.Entry<DockPopout, DockContent> entry : dock.getAllActive().entrySet()) {
if (entry.getKey() != null) {
result.put(entry.getKey(), entry.getValue());
for (DockPopout popout : dock.getPopouts()) {
DockContent active = dock.getActiveContent(popout);
if (active != null) {
result.put(popout, active);
}
}
return result;
Expand Down Expand Up @@ -698,6 +797,16 @@ public Channel getExistingChannel(String channel) {
return channels.get(channel);
}

/**
* Check if the given channel is currently present.
*
* @param channel
* @return
*/
private boolean doesChannelExist(Channel channel) {
return channel != null && (channels.containsValue(channel) || channel == defaultChannel);
}

public Collection<Channel> getExistingChannelsByOwner(String channel) {
List<Channel> result = new ArrayList<>();
for (Channel chan : channels.values()) {
Expand Down Expand Up @@ -740,8 +849,8 @@ public List<Channel> getChannelsOfType(Channel.Type type) {
for (DockContent content : dock.getContents()) {
if (content.getComponent() instanceof Channel) {
Channel chan = (Channel)content.getComponent();
if ((type == null || chan.getType() == type)
&& channels.containsValue(chan) || chan == defaultChannel) {
boolean typeMatches = type == null || type == chan.getType();
if (typeMatches && doesChannelExist(chan)) {
result.add(chan);
}
}
Expand Down Expand Up @@ -798,7 +907,9 @@ public void setInitialFocus(Channel channel) {
if (channel == null) {
channel = getActiveChannel();
}
channel.requestFocusInWindow();
if (channel != null) {
channel.requestFocusInWindow();
}
}
}

Expand Down Expand Up @@ -937,6 +1048,24 @@ public JPopupMenu getContextMenu() {
public void remove() {
channels.channelTabClosed(getContent());
}

@Override
public boolean canPopout() {
/**
* Prevent default channel popout. The default channel should stay
* in main. It can still be moved when tabs are visible in main, but
* it will be moved back if main becomes empty.
*/
return ((Channel)(getComponent())).getRoom() != Room.EMPTY;
}

@Override
public void setTitle(String title) {
if (title.isEmpty()) {
title = "No channel";
}
super.setTitle(title);
}

}

Expand Down
Loading

0 comments on commit 081ac6b

Please sign in to comment.