Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Focus Mode #164

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import static net.silthus.schat.channel.ChannelHelper.ConfiguredSetting.set;
import static net.silthus.schat.channel.ChannelSettings.GLOBAL;
import static net.silthus.schat.channel.ChannelSettings.PROTECTED;
import static net.silthus.schat.ui.view.ViewConfig.FORMAT_CONFIG;
import static net.silthus.schat.ui.views.tabbed.TabFormatConfig.TAB_FORMAT_CONFIG;

public class ChannelSteps {

Expand Down Expand Up @@ -71,6 +71,6 @@ public void configureChannel(ChannelHelper.ConfiguredSetting<?> setting, Channel

@Given("the channel {channel} has a custom format")
public void theChannelGlobalHasACustomFormat(Channel channel) {
channel.get(FORMAT_CONFIG).messageFormat(new MiniMessageFormat("<source_display_name>: <text>"));
channel.get(TAB_FORMAT_CONFIG).messageFormat(new MiniMessageFormat("<source_display_name>: <text>"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public <E extends SChatEvent> E post(@NonNull E event) {
return new HashSet<>();
}

@Override
public void unregister(Object listener) {

}

@Override
public @NonNull @Unmodifiable <E extends SChatEvent> Set<EventSubscription<E>> subscriptions(@NonNull Class<E> eventClass) {
return Set.of();
Expand Down
10 changes: 10 additions & 0 deletions core/src/main/java/net/silthus/schat/eventbus/EventBus.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ static EventBus eventBus(boolean debug) {
*/
@NonNull @Unmodifiable Set<EventSubscription<?>> register(Object listener);

/**
* Unregisters the given listener from this event bus.
*
* <p>All event handlers that are annotated with {@link Subscribe} in the listener are unregistered.</p>
*
* @param listener the listener to unregister
* @since next
*/
void unregister(Object listener);

/**
* Gets a set of all registered handlers for a given event.
*
Expand Down
27 changes: 21 additions & 6 deletions core/src/main/java/net/silthus/schat/eventbus/EventBusImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import net.kyori.event.SimpleEventBus;
import net.silthus.schat.events.SChatEvent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;

@Accessors(fluent = true)
Expand Down Expand Up @@ -68,6 +69,11 @@ public <E extends SChatEvent> E post(final @NonNull E event) {
.collect(Collectors.toUnmodifiableSet());
}

@Override
public void unregister(@NonNull Object listener) {
bus.unregister(sub -> ((EventSubscriptionImpl<?>) sub).owner() == listener);
}

@NotNull
@SuppressWarnings("unchecked")
private EventSubscription<? extends SChatEvent> registerSubscription(Object listener, Method method) {
Expand All @@ -78,20 +84,29 @@ private EventSubscription<? extends SChatEvent> registerSubscription(Object list
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
});
}, listener);
}

private <T extends SChatEvent> EventSubscription<T> registerSubscription(final Class<T> eventClass,
final Consumer<? super T> handler) {
final EventSubscriptionImpl<T> eventHandler = createSubscription(eventClass, handler);
final EventSubscriptionImpl<T> eventHandler = createSubscription(eventClass, handler, null);
this.bus.register(eventClass, eventHandler);

return eventHandler;
}

private <T extends SChatEvent> EventSubscription<T> registerSubscription(final Class<T> eventClass,
final Consumer<? super T> handler,
final @Nullable Object owner) {
final EventSubscriptionImpl<T> eventHandler = createSubscription(eventClass, handler, owner);
this.bus.register(eventClass, eventHandler);

return eventHandler;
}

@NotNull
protected <T extends SChatEvent> EventSubscriptionImpl<T> createSubscription(Class<T> eventClass, Consumer<? super T> handler) {
return new EventSubscriptionImpl<>(this, eventClass, handler);
protected <T extends SChatEvent> EventSubscriptionImpl<T> createSubscription(Class<T> eventClass, Consumer<? super T> handler, @Nullable Object owner) {
return new EventSubscriptionImpl<>(this, eventClass, handler, owner);
}

@Override
Expand Down Expand Up @@ -150,8 +165,8 @@ public <E extends SChatEvent> E post(@NonNull E event) {
}

@Override
protected @NotNull <T extends SChatEvent> EventSubscriptionImpl<T> createSubscription(Class<T> eventClass, Consumer<? super T> handler) {
return new EventSubscriptionImpl.Logging<>(this, eventClass, handler);
protected @NotNull <T extends SChatEvent> EventSubscriptionImpl<T> createSubscription(Class<T> eventClass, Consumer<? super T> handler, Object owner) {
return new EventSubscriptionImpl.Logging<>(this, eventClass, handler, owner);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import lombok.extern.java.Log;
import net.kyori.event.EventSubscriber;
import net.silthus.schat.events.SChatEvent;
import org.jetbrains.annotations.Nullable;

@Getter
@Log(topic = "sChat:EventBus")
Expand All @@ -40,14 +41,17 @@ class EventSubscriptionImpl<E extends SChatEvent> implements EventSubscription<E
private final EventBusImpl eventBus;
private final Class<E> eventClass;
private final Consumer<? super E> handler;
private final @Nullable Object owner;
private final AtomicBoolean active = new AtomicBoolean(true);

EventSubscriptionImpl(final EventBusImpl eventBus,
final Class<E> eventClass,
final Consumer<? super E> consumer) {
final Consumer<? super E> consumer,
final @Nullable Object owner) {
this.eventBus = eventBus;
this.eventClass = eventClass;
this.handler = consumer;
this.owner = owner;
}

@Override
Expand Down Expand Up @@ -81,8 +85,8 @@ public void invoke(final @NonNull E event) {
@Log(topic = "sChat:EventBus")
static final class Logging<E extends SChatEvent> extends EventSubscriptionImpl<E> {

Logging(EventBusImpl eventBus, Class<E> eventClass, Consumer<? super E> consumer) {
super(eventBus, eventClass, consumer);
Logging(EventBusImpl eventBus, Class<E> eventClass, Consumer<? super E> consumer, @Nullable Object owner) {
super(eventBus, eventClass, consumer, owner);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ public void assertReceivedRawMessage(Component component) {
assertThat(receivesMessages).contains(component);
}

public void assertReceivedNoRawMessage() {
assertThat(receivesMessages).isEmpty();
}

public void assertLastRawMessage(Component render) {
assertThat(receivesMessages).last()
.isEqualTo(render);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
import static net.silthus.schat.platform.config.ConfigKeys.CHANNELS;
import static net.silthus.schat.platform.config.ConfigKeys.VIEW_CONFIG;
import static net.silthus.schat.platform.config.TestConfigurationAdapter.testConfigAdapter;
import static net.silthus.schat.ui.view.ViewConfig.FORMAT_CONFIG;
import static net.silthus.schat.ui.views.tabbed.TabFormatConfig.TAB_FORMAT_CONFIG;
import static org.assertj.core.api.Assertions.assertThat;

class ConfigTests {
Expand Down Expand Up @@ -118,15 +118,15 @@ void channel_name_is_loaded_with_color() {
@Test
void loads_message_format() {
final Component format = getTestChannelConfig()
.get(FORMAT_CONFIG)
.get(TAB_FORMAT_CONFIG)
.messageFormat()
.format(View.empty(), message("Hey").source(of(identity("Notch"))).create());
assertThat(format).isEqualTo(text("Notch", AQUA).append(text(": Hey", GREEN)));
}

@Test
void loads_active_color() {
final TextColor activeColor = getTestChannelConfig().get(FORMAT_CONFIG).activeColor();
final TextColor activeColor = getTestChannelConfig().get(TAB_FORMAT_CONFIG).activeColor();
assertThat(activeColor).isEqualTo(RED);
}
}
Expand Down
4 changes: 2 additions & 2 deletions ui/src/main/java/net/silthus/schat/ui/ViewModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
import net.silthus.schat.ui.views.tabbed.TabbedChannelsView;
import net.silthus.schat.util.gson.GsonProvider;

import static net.silthus.schat.ui.view.ViewConfig.FORMAT_CONFIG;
import static net.silthus.schat.ui.view.ViewProvider.cachingViewProvider;
import static net.silthus.schat.ui.views.tabbed.TabFormatConfig.TAB_FORMAT_CONFIG;

@Getter
@Accessors(fluent = true)
Expand Down Expand Up @@ -89,7 +89,7 @@ private void onChatterJoin(ChatterJoinedServerEvent event) {
}

private void configurePrivateChats() {
PrivateChannel.configure(builder -> builder.set(FORMAT_CONFIG, config().privateChatFormat()));
PrivateChannel.configure(builder -> builder.set(TAB_FORMAT_CONFIG, config().privateChatFormat()));
}

private static final class MiniMessageFormatSerializer implements JsonSerializer<Format>, JsonDeserializer<Format> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* This file is part of sChat, licensed under the MIT License.
* Copyright (C) Silthus <https://www.github.com/silthus>
* Copyright (C) sChat team and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.silthus.schat.ui.format;

import lombok.Data;
import lombok.experimental.Accessors;
import net.kyori.adventure.text.Component;
import net.silthus.schat.message.Message;
import net.silthus.schat.message.MessageSource;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
import org.spongepowered.configurate.objectmapping.meta.Setting;

import static net.kyori.adventure.text.Component.text;
import static net.kyori.adventure.text.Component.translatable;
import static net.kyori.adventure.text.format.NamedTextColor.DARK_AQUA;
import static net.kyori.adventure.text.format.NamedTextColor.GRAY;
import static net.kyori.adventure.text.format.NamedTextColor.YELLOW;

@Data
@Accessors(fluent = true)
@ConfigSerializable
public class ChannelFormatConfig {

/**
* Controls the formatting of a channel tab.
*/
public static final net.silthus.schat.pointer.Setting<ChannelFormatConfig> FORMAT_CONFIG = net.silthus.schat.pointer.Setting.dynamicSetting(ChannelFormatConfig.class, "format", ChannelFormatConfig::new);

@Setting("message_format")
private Format messageFormat = (view, msg) ->
msg.get(Message.SOURCE)
.filter(MessageSource.IS_NOT_NIL)
.map(identity -> identity.displayName().colorIfAbsent(YELLOW).append(text(": ", GRAY)))
.orElse(Component.empty())
.append(((Message) msg).text().colorIfAbsent(GRAY));

@Setting("self_message_format")
private Format selfMessageFormat = (view, msg) ->
translatable("schat.chat.message.you").color(DARK_AQUA)
.append(text(": ", GRAY))
.append(((Message) msg).text().colorIfAbsent(GRAY));
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@
import net.silthus.schat.message.Message;
import net.silthus.schat.ui.format.Format;
import net.silthus.schat.ui.format.MiniMessageFormat;
import net.silthus.schat.ui.view.ViewConfig;
import net.silthus.schat.ui.views.tabbed.TabFormatConfig;

public class Replacements {

private final List<ReplacementProvider> replacementProviders = new ArrayList<>();

@Subscribe
public void onChannelMessage(SendChannelMessageEvent event) {
final Format format = event.channel().get(ViewConfig.FORMAT_CONFIG).messageFormat();
final Format format = event.channel().get(TabFormatConfig.TAB_FORMAT_CONFIG).messageFormat();
if (format instanceof MiniMessageFormat miniMessageFormat)
event.message().set(ReplacementProvider.REPLACED_MESSAGE_FORMAT, applyTo(event.message(), miniMessageFormat.format()));
}
Expand Down
9 changes: 3 additions & 6 deletions ui/src/main/java/net/silthus/schat/ui/view/View.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,9 @@

import net.kyori.adventure.text.Component;
import net.silthus.schat.chatter.Chatter;
import net.silthus.schat.pointer.Setting;

import static net.silthus.schat.pointer.Setting.setting;

public interface View {

Setting<Integer> VIEW_HEIGHT = setting(Integer.class, "height", 100); // minecraft chat box height in lines

static View empty() {
return EmptyView.EMPTY;
}
Expand All @@ -42,6 +37,8 @@ static View empty() {
Component render();

default void update() {
chatter().sendRawMessage(render());
final Component render = render();
if (!render.equals(Component.empty()))
chatter().sendRawMessage(render);
}
}
11 changes: 0 additions & 11 deletions ui/src/main/java/net/silthus/schat/ui/view/ViewConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import lombok.Data;
import lombok.experimental.Accessors;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.JoinConfiguration;
import net.silthus.schat.message.Message;
import net.silthus.schat.message.MessageSource;
import net.silthus.schat.ui.format.Format;
Expand All @@ -43,11 +42,6 @@
@ConfigSerializable
public class ViewConfig {

/**
* Controls the formatting of a channel tab.
*/
public static final net.silthus.schat.pointer.Setting<TabFormatConfig> FORMAT_CONFIG = net.silthus.schat.pointer.Setting.dynamicSetting(TabFormatConfig.class, "format", TabFormatConfig::new);

@Setting("height")
private int height = 100;
@Setting("system_message_format")
Expand All @@ -59,9 +53,4 @@ public class ViewConfig {
.append(((Message) msg).text());
@Setting("private_chat_format")
private TabFormatConfig privateChatFormat = new TabFormatConfig();
private transient JoinConfiguration channelJoinConfig = JoinConfiguration.builder()
.prefix(text("| "))
.separator(text(" | "))
.suffix(text(" |"))
.build();
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
import static net.silthus.schat.channel.ChannelSettings.FORCED;
import static net.silthus.schat.ui.util.ViewHelper.formatMessage;
import static net.silthus.schat.ui.util.ViewHelper.subscriptOf;
import static net.silthus.schat.ui.view.ViewConfig.FORMAT_CONFIG;
import static net.silthus.schat.ui.views.tabbed.TabFormatConfig.TAB_FORMAT_CONFIG;

@SuppressWarnings("CheckStyle")
@Getter
Expand Down Expand Up @@ -93,7 +93,7 @@ protected ChannelTab(@NonNull TabbedChannelsView view,
}

private TabFormatConfig config() {
return channel().get(FORMAT_CONFIG);
return channel().get(TAB_FORMAT_CONFIG);
}

@Override
Expand Down
Loading