<commit_before>"""
A web app that allows multiple people to colaborate in painting. People
connecting later will not see the "paint" that was added earlier. Each
person is assigned a random color which affects how that person can
best contribute to the painting.

This app might be running at the demo server: http://flexx1.zoof.io
"""

import random

from flexx import app, ui, event

COLORS = ('#eee', '#999', '#555', '#111', 
          '#f00', '#0f0', '#00f', '#ff0', '#f0f', '#0ff',
          '#a44', '#4a4', '#44a', '#aa4', '#afa', '#4aa',
          )


class Relay(event.HasEvents):
    """ Global object to relay paint events to all participants.
    """
    @event.emitter
    def global_paint(self, pos, color):
        return dict(pos=pos, color=color)


# Create global relay
relay = Relay()


class ColabPainting(ui.Widget):
    """ Web app for colaborative painting.
    """
    
    CSS = """
    .flx-Widget { background: #ddd; }
    .flx-CanvasWidget {
        background: #fff;
        border: 10px solid #000;
        min-width: 400px; max-width: 400px;
        min-height: 400px; max-height: 400px;
    }
    """
    
    def init(self):
        
        # Select random color
        self.color = random.choice(COLORS)
        
        # App layout
        with ui.VBox():
            self.people = ui.Label(flex=0)
            ui.Widget(flex=1)
            with ui.HBox(flex=2):
                ui.Widget(flex=1)
                self.canvas = ui.CanvasWidget(flex=0)
                ui.Widget(flex=1)
            ui.Widget(flex=1)
        
        # Connect events
        self.connect(self._this_user_adds_paint, 'canvas.mouse_down')
        relay.connect(self._any_user_adds_paint, 'global_paint:' + self.id)
        
        # Start people-count-updater
        self._update_participants()
    
    @event.prop
    def color(self, color='#000'):
        """ The selected color for the current session. """
        return str(color)
    
    def _this_user_adds_paint(self, *events):
        """ Detect mouse down, emit global paint event via the relay. """
        for ev in events:
            relay.global_paint(ev.pos, self.color)
    
    def _any_user_adds_paint(self, *events):
        """ Receive global paint event from the relay, emit local paint event. """
        if self.session.status:
            for ev in events:
                self.emit('paint', ev)
    
    def _update_participants(self):
        """ Keep track of the number of participants. """
        if not self.session.status:
            relay.disconnect('new_dot:' + self.id)  # clean up
            return  # and dont't invoke a new call
        proxies = app.manager.get_connections(self.__class__.__name__)
        n = len(proxies)
        del proxies<commit_msg>Fix memory leak in colab painting example<commit_after>"""
A web app that allows multiple people to colaborate in painting. People
connecting later will not see the "paint" that was added earlier. Each
person is assigned a random color which affects how that person can
best contribute to the painting.

This app might be running at the demo server: http://flexx1.zoof.io
"""

import random

from flexx import app, ui, event

COLORS = ('#eee', '#999', '#555', '#111', 
          '#f00', '#0f0', '#00f', '#ff0', '#f0f', '#0ff',
          '#a44', '#4a4', '#44a', '#aa4', '#afa', '#4aa',
          )


class Relay(event.HasEvents):
    """ Global object to relay paint events to all participants.
    """
    @event.emitter
    def global_paint(self, pos, color):
        return dict(pos=pos, color=color)


# Create global relay
relay = Relay()


class ColabPainting(ui.Widget):
    """ Web app for colaborative painting.
    """
    
    CSS = """
    .flx-Widget { background: #ddd; }
    .flx-CanvasWidget {
        background: #fff;
        border: 10px solid #000;
        min-width: 400px; max-width: 400px;
        min-height: 400px; max-height: 400px;
    }
    """
    
    def init(self):
        
        # Select random color
        self.color = random.choice(COLORS)
        
        # App layout
        with ui.VBox():
            self.people = ui.Label(flex=0)
            ui.Widget(flex=1)
            with ui.HBox(flex=2):
                ui.Widget(flex=1)
                self.canvas = ui.CanvasWidget(flex=0)
                ui.Widget(flex=1)
            ui.Widget(flex=1)
        
        # Connect events
        self.connect(self._this_user_adds_paint, 'canvas.mouse_down')
        relay.connect(self._any_user_adds_paint, 'global_paint:' + self.id)
        
        # Start people-count-updater
        self._update_participants()
    
    @event.prop
    def color(self, color='#000'):
        """ The selected color for the current session. """
        return str(color)
    
    def _this_user_adds_paint(self, *events):
        """ Detect mouse down, emit global paint event via the relay. """
        for ev in events:
            relay.global_paint(ev.pos, self.color)
    
    def _any_user_adds_paint(self, *events):
        """ Receive global paint event from the relay, emit local paint event. """
        if self.session.status:
            for ev in events:
                self.emit('paint', ev)
    
    def _update_participants(self):
        """ Keep track of the number of participants. """
        if not self.session.status:
            relay.disconnect('global_paint:' + self.id)  # clean up
            return  # and dont't invoke a new call
        proxies = app.manager.get_connections(self.__class__.__name__)
        n = len(proxies)
        del proxies
{'fraction_non_alphanumeric': 0.11887232896390734, 'fraction_numerical': 0.019393068773567966, 'mean_word_length': 2.173789173789174, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4920237', 'n_tokens_mistral': 1777, 'n_tokens_neox': 1700, 'n_words': 487}